ArrayList cities = new ArrayList<Integer>();
while (cities.size() != 0) { //while not null (ATM it will always be null till finished writing the loop)
int s = 0;
int one = cities.get(d); //this is where I get the error
cities.add(s,"x");
System.out.println("___________");
System.out.println(cities.get(s));
}

I get an error: Found Object, required int
(Please note that there may be problems with my loops (its not finished yet) All i need to know is how to get the element d or even element 0 or 1 to save as type int in variable "one"

Any thoughts?

Thanks!

11-22-2008, 06:41 AM

emceenugget

ArrayList holds Objects only. You are trying to get an int from an ArrayList. That is why you get your error.

or something like that. As you say, you are still working on the code.

Thanks for that.
I'm not sure if it what I am looking for:
What I want to do is save the 1st element of the arrayList to an int variable.I dont mind changing the type of the arrayList, eg back to a String

Here is more complete code:

Quote:

ArrayList cities = new ArrayList<Integer>();
x=1
s=0
cities.add(s,"x"); // add "1" at position 0
// now I want to save whatever is at positin s to an int variable
/i tried: int number = cities.get(s);
but the complier says I cant due to incompatable file types/

I couldnt seem to get the above sugestions to work.

Thanks so far!

11-22-2008, 03:32 PM

Fubarable

Your code is not kosher and in fact contains a gross error.

You first create an ArrayList of Integer called cities, and then add a String object to this list via this call:

Code:

cities.add(s,"x");

Why are you doing this?

You know of course that "x" is a string and has absolutely no relationship to the variable x that holds an int, correct?

11-22-2008, 03:35 PM

grahamb314

Quote:

Originally Posted by Fubarable

Your code is not kosher and in fact contains a gross error.

You first create an ArrayList of Integer called cities, and then add a String object to this list via this call:

Code:

cities.add(s,"x");

Why are you doing this?

You know of course that "x" is a string and has absolutely no relationship to the variable x that holds an int, correct?

Either way I think the problem remains, I think this should solve the confusion?

Code:

ArrayList cities = new ArrayList<Integer>();
int x=1
s=0
cities.add(s,x); // add "1" at position 0
// now I want to save whatever is at positin s to an int variable
//i tried: int number = cities.get(s);
//but the complier says I cant due to incompatable file types

11-22-2008, 03:41 PM

Fubarable

At this point, I recommend that you show us a small compilable program that reproduces your problem. Something on this order would help us help you:

It's casting the object returned from the cities ArrayList into an Integer. But I'm confused as this should be completely unnecessary if you are using a generic ArrayList that was declared ArrayList<Integer> to begin with.

11-22-2008, 06:09 PM

grahamb314

Quote:

Originally Posted by Fubarable

It's casting the object returned from the cities ArrayList into an Integer. But I'm confused as this should be completely unnecessary if you are using a generic ArrayList that was declared ArrayList<Integer> to begin with.

That same confusion was why I posted :)

this seems to have done the trick and thanks for explaining the casting!