0

need help again. sorry. how can i remove the null values from my array? here is what i got so far.

int unikCount = 0;
String c = " ";
for (int i = 0; i < a.length; i++) {
    for (int j = 0; j < a.length; j++) {
        if (tempAry[i].equals(tempAry[j])) {
            unikCount++;
        }
        if (unikCount > 1) {
            tempAry[j] = c;
            unikCount = 1;

        }

    }
    unikCount = 0;
}
for (i = 0; i < a.length; i++) {
    if (tempAry[i] != c) {
        unikCount++;
    }

}
System.out.println(unikCount);

for (int i = 0; i < a.length; i++) {
    for (int j = 1; j < a.length; j++) {
        if (tempAry[i].equals(tempAry[j])) {
            if (tempAry[i] == c) {
                count++;
                if (tempAry[j] != c) {
                    count++;
                    tempAry[j] = tempAry[i];

                }
            }
        }
    }

}
count = 0;
for (int i = 0; i < a.length; i++) {
    System.out.println(tempAry[i]);
}

*the remove part is after the "System.out.println(unikCount)". thanks for the upcoming help. by the way, cant use hash and arraylist.

12
  • Is there a reason why you can't just create a new array and copy over the non-null values? Commented Oct 4, 2013 at 23:21
  • Do you mean shorten an array by consolidating it by removing the buckets that have the NULL value in them? And is there a reason why you aren't using a List instead of an array? Lists handily manage this for you. Commented Oct 4, 2013 at 23:21
  • Dont compare string with == instead use String.equals(). See Java String.equals versus == Commented Oct 4, 2013 at 23:22
  • @Smit Although this is usually correct, in this case seems valid using == since he MAY want to reference every single c and not every " ", what if his array already contains a whitespace? equals will return true for that. Commented Oct 4, 2013 at 23:23
  • Are you allowed to use ArrayList? Commented Oct 4, 2013 at 23:23

3 Answers 3

2

You can check for null like this:

if ( null == someObject ) 
{
    // do things
}

There is no way to remove an element from an array and have it shrink automatically. You'll have to use a temporary array to hold values, create an array of a new size and transfer all those items.

A more effective way would be to use a List.

Sign up to request clarification or add additional context in comments.

Comments

0

Look at your logic (with proper indentation):

if(tempAry[i].equals(tempAry[j])) {                      
    if(tempAry[i] == c) {           
       count++;
       if(tempAry[j] != c) {
           count++;
           tempAry[j] = tempAry[i];

       }
    }
}

It does not make any sense. Why would you check for tempAry[j] != c inside if(tempAry[i] == c) ???

Did you mean to use if...else instead?

Comments

0
int j = 0;
Object[] temp = new Object[a.length];
for (int i = 0; i < a.length; i++) {
    if (a[i] != null) {
        temp[j++] = a[i];
    }
}
Object[] newA = new Object[j];
System.arraycopy(temp, 0, newA, 0, j);

If the array is an array of, eg, String, you'd of course change "Object" to "String". And if by "null" one means an empty String, then the if test would be changed appropriately.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.