从数组中删除特定值(java)
我有整数a=4和数组b7,8,9,4,3,4,4,2,1 我必须编写一个方法,从数组b中删除int ALL a 期望结果7,8,9,3,2,1 这就是我到目前为止所做的从数组中删除特定值(java),java,arrays,Java,Arrays,我有整数a=4和数组b7,8,9,4,3,4,4,2,1 我必须编写一个方法,从数组b中删除int ALL a 期望结果7,8,9,3,2,1 这就是我到目前为止所做的 public static int[] removeTwo (int x, int[] array3) { int counter = 0; boolean[] barray = new boolean [array3.length]; for (int k=0; k<array3.leng
public static int[] removeTwo (int x, int[] array3)
{
int counter = 0;
boolean[] barray = new boolean [array3.length];
for (int k=0; k<array3.length; k++)
{
barray[k] = (x == array3[k]);
counter++;
}
int[] array4 = new int [array3.length - counter];
int num = 0;
for (int j=0; j<array3.length; j++)
{
if(barray[j] == false)
{
array4[num] = array3[j];
num++;
}
}
return array4;
Java结果:1
任何帮助都将不胜感激 在这一行:
int[] array4 = new int [array3.length - counter];
创建一个大小为0的数组,因为此时计数器
等于数组3。长度
这意味着您无法访问该数组中的任何索引。错误源于此for循环:
for (int k=0; k<array3.length; k++)
{
barray[k] = (x == array3[k]);
counter++;
}
要回答您在评论中的问题,应调用该方法,并可按如下方式进行检查:
public static void main(String[] args) {
int[] array3 = {0,1,3,2,3,0,3,1};
int x = 3;
int[] result = removeTwo(x, array3);
for (int n : result) {
System.out.print(""+ n + " ");
}
}
您正在创建
int[] array4 = new int [array3.length - counter];// 0 length array.
那里不能有0th
索引。至少长度应1
才能有0th
索引
根据我的建议,最好使用
List
。那么你可以很容易地做到 实际上,数组对于作业来说是错误的工具,因为除了其他工具之外,您最终将得到无法删除的零散值。只需使用一个ArrayList,它提供了一个removeAll()方法来执行您需要的操作。如果您确实需要阵列,您甚至可以执行以下操作:
List<Integer> list = new ArrayList(Arrays.asList(array))
list.removeAll(4);
array = list.toArray();
List List=newarraylist(Arrays.asList(array))
列表。移除所有(4);
array=list.toArray();
(精确的方法名称/参数可能需要调整,因为这都来自内存)。最简单的方法是使用第二个数组,在其中输入正确的值 像那样的东西
public static int[] removeTwo (int x, int[] array3)
{
int counter = 0;
int[] array4 = new int[array3.lenght];
for (int i = 0; i < array3.lenght; i ++) {
if(array3[i] == x){
array4[counter] = array3[i];
}
}
return array4;
}
公共静态int[]removeTwo(int x,int[]array3)
{
int计数器=0;
int[]数组4=新的int[array3.长度];
对于(int i=0;i
另一种方法是从
数组3
中删除x
计算值,并将后面的值向前移动从数组中删除元素的最佳方法是使用迭代器使用List
。试试看
Integer[] array = {7, 8, 9, 4, 3, 4, 4, 2, 1};
List<Integer> list = new ArrayList(Arrays.asList(array));
for(Iterator<Integer> it=list.iterator();it.hasNext();){
if(it.next()==4){
it.remove();
}
}
Integer[]数组={7,8,9,4,3,4,4,2,1};
List List=newarraylist(Arrays.asList(array));
for(Iterator it=list.Iterator();it.hasNext();){
if(it.next()==4){
it.remove();
}
}
你在第一个for循环的每一步上递增计数器,这真的是你的意思吗?你为什么不使用ListArray有什么好的理由吗?在列表中有什么方法removeAll(int)吗?int是一个基本对象,你不能把它放在列表
@TimB中这就是他为什么使用整数
@Stone我知道,我告诉马苏德:)这个代码不起作用,你从不增加计数器,而且你还两次拼错了长度。对不起,你是对的。array4[counter]=array3[i]之后
您必须增加计数器计数器+=1
它消除了错误,但我仍然在数组int[]array3={0,1,3,2,3,0,3,1}中得到int x;int x=3;移除WO(x,阵列3);对于(int值:array3)System.out.printf(“%d”,值);我在我的主要方法中使用它吗?在你的主要方法中使用什么?为什么要打印array3的值,它似乎是原始数组?等等,我没想清楚。哈哈哈,我需要睡觉,谢谢你的帮助!当然,这是常有的事!
public static int[] removeTwo (int x, int[] array3)
{
int counter = 0;
int[] array4 = new int[array3.lenght];
for (int i = 0; i < array3.lenght; i ++) {
if(array3[i] == x){
array4[counter] = array3[i];
}
}
return array4;
}
Integer[] array = {7, 8, 9, 4, 3, 4, 4, 2, 1};
List<Integer> list = new ArrayList(Arrays.asList(array));
for(Iterator<Integer> it=list.iterator();it.hasNext();){
if(it.next()==4){
it.remove();
}
}