Results 1 to 10 of 10
- 11-22-2008, 03:20 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 13
- Rep Power
- 0
[SOLVED] ArrayList element to int
Hi all,
I am trying to get a certain element of my ArrayList to an int:
I get an error: Found Object, required intJava Code: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)); }
(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, 05:41 AM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
ArrayList holds Objects only. You are trying to get an int from an ArrayList. That is why you get your error.
Integer is a wrapper class for the the int primitive.
- 11-22-2008, 01:07 PM #3
or something like that. As you say, you are still working on the code.Java Code:int city; //......some code..... Integer nextCity = ( Integer ) arrayList.getSomeCity(index); city = nextCity.toInt();
Last edited by Nicholas Jordan; 11-22-2008 at 01:08 PM. Reason: class object Integer, not ( int ) in the cast as I had it.
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 11-22-2008, 02:32 PM #4
Member
- Join Date
- Nov 2008
- Posts
- 13
- Rep Power
- 0
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:
I couldnt seem to get the above sugestions to work.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/
Thanks so far!
-
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:
Why are you doing this?Java Code:cities.add(s,"x");
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, 02:35 PM #6
Member
- Join Date
- Nov 2008
- Posts
- 13
- Rep Power
- 0
Either way I think the problem remains, I think this should solve the confusion?
Java 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
-
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:
Java Code:import java.util.ArrayList; import java.util.List; public class TestArrayList { private static final int MAX = 10; public static void main(String[] args) { List<Integer> cities = new ArrayList<Integer>(); for (int i = 0; i < MAX; i++) { cities.add(i); } for (int i = 0; i < cities.size(); i++) { int myInt = cities.get(i); System.out.println(myInt); } } }
- 11-22-2008, 04:05 PM #8
Member
- Join Date
- Nov 2008
- Posts
- 13
- Rep Power
- 0
Hi all,
this seems to have done the trick!
Although I dont know what it is doing?Java Code:Integer curr = ( Integer ) cities.get(i);
Cheers!
-
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, 05:09 PM #10
Member
- Join Date
- Nov 2008
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
How to access element in a CSV?
By venkatteshb in forum New To JavaReplies: 11Last Post: 08-17-2008, 11:37 AM -
how can we get the element of by using the hashtable
By raj reddy in forum Web FrameworksReplies: 1Last Post: 05-06-2008, 01:45 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
Max element in an Array
By mew in forum New To JavaReplies: 5Last Post: 12-03-2007, 05:26 PM -
a no such element exception
By headlice1 in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:36 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks