在java中制作从数组中删除零的方法

在java中制作从数组中删除零的方法,java,Java,我很好奇如何编写一个从数组中删除所有零的方法。如果我在main方法中有数组。例如,我的主要方法如下所示 public static void main(String[] args) { int[] test = {1, 0, 4, 7, 0, 2, 10, 82, 0}; System.out.println(Arrays.toString(test) + ": length = " + test.length); int[] result = removeZeros(test);

我很好奇如何编写一个从数组中删除所有零的方法。如果我在main方法中有数组。例如,我的主要方法如下所示

  public static void main(String[] args) {
 int[] test = {1, 0, 4, 7, 0, 2, 10, 82, 0};
 System.out.println(Arrays.toString(test) +  ": length = " + test.length);
 int[] result = removeZeros(test);
 System.out.println(Arrays.toString(result) + ": length = " + result.length);
 }
并让代码输出长度和不带零的数组,如:

 [1, 0, 4, 7, 0, 2, 10, 82, 0]: length = 9
 [1, 4, 7, 2, 10, 82]: length = 6
除了这样做,我不知道如何为此编写方法:

  int[] test = {1, 0, 4, 7, 0, 2, 10, 82, 0};
    int length = 0;
    for (int i=0; i<test.length; i++){
        if (test[i] != 0)
            length++;
    }
    int [] intResult = new int[length];
    for (int i=0, j=0; i<test.length; i++){
        if (test[i] != 0) {
            intResult[j] = test[i];
            j++;
        }
    }
int[]test={1,0,4,7,0,2,10,82,0};
整数长度=0;
对于(int i=0;i
有没有办法让这个方法打印出原始数组和新数组,其中没有零+长度

没有明显更好的方法来删除零。显然,您可以将其放在方法中…如果您想这样做的话。该方法需要创建并返回新数组。(您不能更改传递给该方法的数组参数的大小…)

要打印数组,可以使用循环迭代并打印元素,也可以使用
Arrays.toString(array)
并输出字符串

要打印数组的长度,请打印
array.length

有没有办法让这个方法打印出原始数组和新数组,其中没有零+长度

没有明显更好的方法来删除零。显然,您可以将其放在方法中…如果您想这样做的话。该方法需要创建并返回新数组。(您不能更改传递给该方法的数组参数的大小…)

要打印数组,可以使用循环迭代并打印元素,也可以使用
Arrays.toString(array)
并输出字符串


要打印数组的长度,请打印
array.length

我没有测试它,但这应该可以:

public class Blah {

public static void main(String[] args) {
 int[] test = {1, 0, 4, 7, 0, 2, 10, 82, 0};
 System.out.println(Arrays.toString(test) +  ": length = " + test.length);
 int[] result = removeZeros(test);
 System.out.println(Arrays.toString(result) + ": length = " + result.length);
 }

public int[] removeZeros(int[] test) {
    int length = 0;
for (int i=0; i<test.length; i++){
    if (test[i] != 0)
        length++;
}
int [] intResult = new int[length];
for (int i=0, j=0; i<test.length; i++){
    if (test[i] != 0) {
        intResult[j] = test[i];
        j++;
    }
 return intResult;
}

}
公共类废话{
公共静态void main(字符串[]args){
int[]test={1,0,4,7,0,2,10,82,0};
System.out.println(Arrays.toString(test)+“:length=“+test.length”);
int[]结果=删除零(测试);
System.out.println(Arrays.toString(result)+“:length=“+result.length”);
}
公共int[]删除零(int[]测试){
整数长度=0;

对于(int i=0;i我没有测试它,但这应该可以:

public class Blah {

public static void main(String[] args) {
 int[] test = {1, 0, 4, 7, 0, 2, 10, 82, 0};
 System.out.println(Arrays.toString(test) +  ": length = " + test.length);
 int[] result = removeZeros(test);
 System.out.println(Arrays.toString(result) + ": length = " + result.length);
 }

public int[] removeZeros(int[] test) {
    int length = 0;
for (int i=0; i<test.length; i++){
    if (test[i] != 0)
        length++;
}
int [] intResult = new int[length];
for (int i=0, j=0; i<test.length; i++){
    if (test[i] != 0) {
        intResult[j] = test[i];
        j++;
    }
 return intResult;
}

}
公共类废话{
公共静态void main(字符串[]args){
int[]test={1,0,4,7,0,2,10,82,0};
System.out.println(Arrays.toString(test)+“:length=“+test.length”);
int[]结果=删除零(测试);
System.out.println(Arrays.toString(result)+“:length=“+result.length”);
}
公共int[]删除零(int[]测试){
整数长度=0;

对于(inti=0;i,只需对您自己的代码进行最细微的更改,就可以将其作为一种方法

int [] removeZeros(int [] test);
{
    if (test == null) {
        return null;
    }

    int length = 0;
    for (int i=0; i<test.length; i++){
        if (test[i] != 0)
            length++;
    }

    int [] intResult = new int[length];

    for (int i=0, j=0; i<test.length; i++){
        if (test[i] != 0) {
            intResult[j] = test[i];
            j++;
        }
    }

    return intResult;
}
int[]删除零(int[]测试);
{
if(test==null){
返回null;
}
整数长度=0;

对于(inti=0;i,只需对您自己的代码进行最细微的更改,就可以将其作为一种方法

int [] removeZeros(int [] test);
{
    if (test == null) {
        return null;
    }

    int length = 0;
    for (int i=0; i<test.length; i++){
        if (test[i] != 0)
            length++;
    }

    int [] intResult = new int[length];

    for (int i=0, j=0; i<test.length; i++){
        if (test[i] != 0) {
            intResult[j] = test[i];
            j++;
        }
    }

    return intResult;
}
int[]删除零(int[]测试);
{
if(test==null){
返回null;
}
整数长度=0;
对于(int i=0;i使用Java 8:

int[] test = {1, 0, 4, 7, 0, 2, 10, 82, 0}
int[] result = Arrays.stream(test).filter(i -> i != 0).toArray();
使用Java 8:

int[] test = {1, 0, 4, 7, 0, 2, 10, 82, 0}
int[] result = Arrays.stream(test).filter(i -> i != 0).toArray();
这个怎么样

  • 使用相同长度的数组输入创建数组结果
  • 使用可变长度计算预期结果的长度
  • 如果输入的当前元素大于零,则结果[长度]=
    输入测试的当前元素[i]和长度++
  • 如果长度大于0,则使用长度值剪切数组结果
守则:

int[] test = {1, 0, 4, 7, 0, 2, 10, 82, 0};
int[] intResult = new int[test.length];
int length = 0;
for (int i=0; i<test.length; i++){
    if (test[i] > 0) {
        intResult[length] = test[i];
        length++;
    }
}
if(length > 0)intResult = Arrays.copyOf(intResult, length);
int[]test={1,0,4,7,0,2,10,82,0};
int[]intResult=新的int[test.length];
整数长度=0;
对于(int i=0;i 0){
intResult[长度]=试验[i];
长度++;
}
}
如果(长度>0)intResult=Arrays.copyOf(intResult,长度);
这个怎么样

  • 使用相同长度的数组输入创建数组结果
  • 使用可变长度计算预期结果的长度
  • 如果输入的当前元素大于零,则结果[长度]=
    输入测试的当前元素[i]和长度++
  • 如果长度大于0,则使用长度值剪切数组结果
守则:

int[] test = {1, 0, 4, 7, 0, 2, 10, 82, 0};
int[] intResult = new int[test.length];
int length = 0;
for (int i=0; i<test.length; i++){
    if (test[i] > 0) {
        intResult[length] = test[i];
        length++;
    }
}
if(length > 0)intResult = Arrays.copyOf(intResult, length);
int[]test={1,0,4,7,0,2,10,82,0};
int[]intResult=新的int[test.length];
整数长度=0;
对于(int i=0;i 0){
intResult[长度]=试验[i];
长度++;
}
}
如果(长度>0)intResult=Arrays.copyOf(intResult,长度);

您的代码似乎没问题,我不明白您有什么问题facing@njzk2我想我只是好奇这是否可行哈哈,所以我所要做的就是把它放在一个方法中?:)?@njzk2我写的代码在main方法中,我想把它放在一个单独的方法中..如果这有意义的话,你只需要一个方法
public int[]removeZeros(int[]input){}
您的代码似乎还可以,我不明白您有什么问题facing@njzk2我想我只是好奇这是否管用哈哈,所以我所要做的就是把它放在一个方法里?:)?@njzk2我写的代码在main方法中,我想把它放在一个单独的方法中..如果这有意义的话,你只需要一个方法
public int[]removeZeros(int[]input){}