Tuesday, July 28, 2009

ArrayList Problem(#2)?

import java.util.ArrayList;





class CheckArrayList


{


public static void main(String[] arg)


{


ArrayList list = new ArrayList();





display(list);


list.add("A");


list.add(0, "B");


list.add(1, "C");


list.add(new Integer(25));


display(list);





}


static void display(ArrayList list)


{


System.out.println("The size of the list is " + list.size());


System.out.println("The list is empty " + list.isEmpty());


System.out.println();





for (int i = 0; i %26lt; list.size(); i++)


{


Object o = list.get(i);


if(o instanceof String)


System.out.println("This object" + (String)o + " is a string " );


else if(o instanceof Integer)


System.out.println("This object" + (Integer)o + "is an integer" );


}


}


}


^Compiles ok. But when running the program, I get a bizarre error...not sure how to correct this problem. Just need the output to work and i'll be fine...any input suggestions are wellcome.

ArrayList Problem(#2)?
you aren't declaring the generic type. It should be like this:





ArrayList%26lt;String%26gt; list = new ArrayList%26lt;String%26gt;();





that's what the -xlint is from, you're trying to put Strings an an array list that is accepting Objects, it works because String inherits from Object but its dangerous. The rest is just a compiler specific issue. The answer above mine is wrong, btw. Nothing in java is declared var list... unless you have a class named var, in which case it should be Var.





What you're trying to do is dump integers and Strings into one ArrayList. You can't do this but you can work around it by making everything a String. So instead of:





list.add(new Integer(25));





use:





list.add("" + 25);





that will add it as a String.





When you're retrieving the values, use Integer.parseInt() to determine if its a number like this:





String o = list.get(i);


try


{


Interger.parseInt(o);


System.out.println("This object " + o + " is a number");


}


catch(NumberFormatException nfe)


{


System.out.println("This object " + 0 + " is a String");


}





it tries parseInt(), and if it doesn't throw anything, the number is an int so it prints out the first message. If parseInt can't work because the String isn't a number, it throws a NumberFormatException, which you catch an then print out the second message.





the reason "o instace of Integer" doesn't work is because everything in your array is an instance of Object. The reason this doesn't work is that String is an instance of Object, but Object is not an instance of String. You're trying to go down the inheritance tree which is impossible.





Hope this helps!
Reply:shouldn't it be var list = New ArrayList(); not ArrayList list = ....?





I don't see any declaration for ArrayList...
Reply:Could it be your classpath? What version of the JRE do you use?
Reply:Your code works fine in Netbeans.





Maybe its an IDE problem. What enviroment are you developing in?


No comments:

Post a Comment