0

I have a code in Java:

for(Iterator it = c.getArrayList().iterator(); it.hasNext(); ) {
    Object i = it.next();

    // Here I have an error, i is not a boolean
    if (i) {
        System.out.format("Delete %s%n", i);
        it.remove();
    }
    else {
        System.out.println("End");
        break;
    }
}

But the if clause throws an error. It expects boolean but Object is given. Java cannot transform types, right?

How do I have to change the type or what do I have to put in the if clause to make it work right?

UPD:

It is a collection (an ArrayList) of Strings.

Object i = it.next();   // I get one element from collection.
if (i) {   // check if it's not the end, if it's not the last element of the collection
Green
  • 28,742
  • 61
  • 158
  • 247
  • What is that supposed to mean? – SLaks Dec 26 '12 at 14:23
  • 1
    What do you want to check it against? – Rohit Jain Dec 26 '12 at 14:23
  • Traverse the collection removing specific elements. I just want to check if the next element exests then delete it. – Green Dec 26 '12 at 14:23
  • 1
    @Green.. And what is that specific element supposed to be? On what basis are you removing it? – Rohit Jain Dec 26 '12 at 14:24
  • 1
    What exactly are you trying to check? Please be more specific. –  Dec 26 '12 at 14:24
  • Do you really want to stop on a `null` value or do you want to print "End" only when it is the end? I suspect you want to delete null values too. – Peter Lawrey Dec 26 '12 at 14:32
  • @Peter Lawrey, I just want to stop when it is the end, when no elements (strings) are in collection – Green Dec 26 '12 at 14:36
  • 1
    But you want to keep `null` values in the collection? – irrelephant Dec 26 '12 at 14:37
  • @irrelephant, in fact I don't know, I'm just trying to undestand the Collection Framework, the example is from here http://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html, Iterators part `static void filter(Collection> c) {` :) – Green Dec 26 '12 at 14:39

7 Answers7

3

Are you trying to test whether it's null?

if (i != null) {
    System.out.format("Delete %s%n", i);
    it.remove();
}
else {
    System.out.println("End");
    break;
}

If you're checking the boolean value of i and the ArrayList is a List<Boolean>, then you can use Iterator<Boolean> it, but you still have to be careful of null pointer exceptions.


Edit: If you want to print End when all the non-null strings are removed, then you can delete the else block and just print End after the for loop.

irrelephant
  • 4,091
  • 2
  • 25
  • 41
1

Luckly Iterator has a hasNext() function.

So you would use:

Object i = null;
if(it.hasNext()){
    i = it.next();
}else{
    //done.
}
//Do stuff with i.
cjds
  • 8,268
  • 10
  • 49
  • 84
Matt Klooster
  • 717
  • 1
  • 5
  • 13
1

Unclear what do you want to test.

If you want to test, whether object is present, then check for null:

if (i==null)

If you want to check some property, then cast to specific type

if(((MyClass)i).iDontWantLoLive()) 

If the objects are booleans, then cast to boolean:

if((Boolean)i)  
Dims
  • 47,675
  • 117
  • 331
  • 600
1

Java is a strongly typed language. If you want/expect boolean values, then do something like this...

        List<Boolean> list = new ArrayList<Boolean>();
    for (Iterator<Boolean> it = list.iterator(); it.hasNext(); ) {
        boolean i = it.next();

        if (i) {
            System.out.format("Delete %s%n", i);
            it.remove();
        }
        else {
            System.out.println("End");
            break;
        }
    }

Otherwise if you're just trying to remove something based on it being null then ('if (i==null)' as others have suggested).

Sean Dawson
  • 232
  • 3
  • 13
1

Your output of "End" suggests you want to do this at the end. You can do this instead.

List list = c.getArrayList();
for(Object o: list)
    System.out.println("Delete " + o);
list.clear();
System.out.println("End");

Note: format just returns a String which you are discarding.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

This is a very slow way to remove the last element of a list. Instead, use

list.remove(list.size() - 1);
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

Yes, Java will not automatically coerce types. You will have to manually cast the Object to a Boolean.

Thorn G
  • 12,620
  • 2
  • 44
  • 56