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.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • Is there a reason why you can't just create a new array and copy over the non-null values? – Dennis Meng Oct 04 '13 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. – Drizzt321 Oct 04 '13 at 23:21
  • Dont compare `string` with `==` instead use `String.equals()`. See [Java String.equals versus ==](http://stackoverflow.com/questions/767372/java-string-equals-versus) – Smit Oct 04 '13 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. – porfiriopartida Oct 04 '13 at 23:23
  • Are you allowed to use `ArrayList`? – PM 77-1 Oct 04 '13 at 23:23
  • Arrays have fixed length so you are not able to just remove its element. If you want to remove nulls you will have to fill elements with null with some other value. Or do you want to create another array and just copy to it non-null values? – Pshemo Oct 04 '13 at 23:24
  • @edgar-miguel-santos-cuevas You should try to isolate the problem yourself, we don't need all the code. Can you rewrite your code with an array where you have nulls and then try to remove them there? – porfiriopartida Oct 04 '13 at 23:26
  • @porfiriopartida I wouldn't trust this. This isn't a good way to distinguish "legitimate" occurrences of `c` from "special" ones that you're using to mark some condition, because Java may make them the same reference anyway. I don't know the rules about when it does or doesn't. Even if _you_ know the rules, I don't, and if I have to read your code I will be scratching my head trying to figure it out. So in my opinion it's just best not to do this. I don't know why the OP isn't setting it to `null` anyway. – ajb Oct 04 '13 at 23:27
  • @porfiriopartida I put that comment just on assumption that OP wants to compare Actual string. – Smit Oct 04 '13 at 23:28
  • @ajb Totally agree, actually he is asking to remove nulls from his array, but there is no null there. He may want to set the undesirable objects to null. – porfiriopartida Oct 04 '13 at 23:29
  • @Smit my assumption is that he wants to "flag" some undesirables objects using a whitespace instead of a null. If he uses a null it would be much more clear as ajb suggests. Seems that he wants to code a Set like list. – porfiriopartida Oct 04 '13 at 23:30
  • @porfiriopartida If we dont have enough information and answering the question just on assumption will not make any sense here. OP need to provide more info and code about what he/she wants to achieve. – Smit Oct 04 '13 at 23:33

3 Answers3

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.

Justin Jasmann
  • 2,363
  • 2
  • 14
  • 18
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?

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
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.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151