简体   繁体   English

从数组中删除零值

[英]Remove zero values from array

I have an array with zero values, and I wish to copy this array to other array but without the zero values. 我有一个零值的数组,我希望将此数组复制到其他数组,但不包含零值。 How can I do this if determining the array size is not possible because I do not know how many zeros are there. 如果无法确定数组大小,因为我不知道有多少个零,该怎么办。 Please note that I can not use List or ArrayList because of reasons. 请注意,由于某些原因,我无法使用List或ArrayList

// frame is the original array

final int[] sorted = new int[??];

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

Yo can do this if it must use array: 如果必须使用数组,则可以执行此操作:

int j = 0;

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

final int[] sorted = new int[j];
j = 0;
for (int i = 0; i < frame.length; i++) {
    if (frame[i]!=0) {
        sorted[j] = frame[i];
        j++;
    }
}

The only way I know how to do this with your given constraints would be to COUNT FIRST, Process NEXT: 我知道如何在给定的约束下进行此操作的唯一方法是先进行计数,然后进行处理:

unsigned int nonZeroCount = 0;

// Count the amount of non-zero values
for (int i = 0; i < frame.length; i++){
    if (frame[i]!=0)
        nonZeroCount++;
}

// Create the NEW Array
final int[] sorted = new int[nonZeroCount];

// NEXT add them to your new array, Need to have 2 separate counters, 1 for your initial array,
// Another for where you are placing it within your new array
unsigned int anotherCount = 0;
for (int i = 0; i < frame.length; i++){
    if (frame[i]!=0){
        sorted[anotherCount ] = frame[i];
        anotherCount++;
    }
}

Try the following: 请尝试以下操作:

String validNumbers = "";

for (int i = 0; i < frames.length; i++) {
 if (frames[i] != 0)
   validNumbers += (i > 0 ? ";": "")+frames[i];
}

String sorted[] = validNumbers.split(";");

This way, You'll have an array of String with the numbers that are different of 0; 这样,您将获得一个String数组,其数字不同,为0;

I do not know much zeros there is. 我不知道那里有多少零。

So why don't you fond out? 那你为什么不喜欢呢?

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

You could make a new array with the same size like the one you test. 您可以制作一个与测试的阵列大小相同的新阵列。 After that you iterate over the array and for each non zero value you found, you add it, while increasing a counter that will represent the number of non zero elements found. 之后,遍历数组,并为找到的每个非零值添加它,同时增加一个计数器,该计数器将表示找到的非零元素的数量。 After this you can use it to create a new array where to copy your values or simple use the counter as a upper bound when iterating the new array. 之后,您可以使用它来创建一个新的数组,在其中复制您的值,或者在迭代新数组时简单地将计数器用作上限。

int noneZeroCount= 0;

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

final int[] sorted = new int[noneZeroCount]; 

// You got size of non zero elements in array now run copy loop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM