Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-22-2008, 04:20 AM
Member
 
Join Date: Nov 2008
Posts: 8
Rep Power: 0
grahamb314 is on a distinguished road
Default [SOLVED] ArrayList element to int
Hi all,

I am trying to get a certain element of my ArrayList to an int:
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));
    }
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!
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-22-2008, 06:41 AM
Senior Member
 
Join Date: Sep 2008
Posts: 564
Rep Power: 2
emceenugget is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-22-2008, 02:07 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 1,018
Rep Power: 3
Nicholas Jordan is on a distinguished road
Default
Code:
int city;
//......some code.....
Integer nextCity = ( Integer ) arrayList.getSomeCity(index);
city = nextCity.toInt();
or something like that. As you say, you are still working on the code.
__________________
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Last edited by Nicholas Jordan; 11-22-2008 at 02:08 PM. Reason: class object Integer, not ( int ) in the cast as I had it.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-22-2008, 03:32 PM
Member
 
Join Date: Nov 2008
Posts: 8
Rep Power: 0
grahamb314 is on a distinguished road
Default
Originally Posted by Nicholas Jordan View Post
Code:
int city;
//......some code.....
Integer nextCity = ( Integer ) arrayList.getSomeCity(index);
city = nextCity.toInt();
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!
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-22-2008, 03:32 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,491
Rep Power: 8
Fubarable is on a distinguished road
Default
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?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-22-2008, 03:35 PM
Member
 
Join Date: Nov 2008
Posts: 8
Rep Power: 0
grahamb314 is on a distinguished road
Default
Originally Posted by Fubarable View Post
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
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-22-2008, 03:41 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,491
Rep Power: 8
Fubarable is on a distinguished road
Default
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:

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);
    }
    
  }
}
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-22-2008, 05:05 PM
Member
 
Join Date: Nov 2008
Posts: 8
Rep Power: 0
grahamb314 is on a distinguished road
Default
Hi all,
this seems to have done the trick!

Code:
Integer curr = ( Integer ) cities.get(i);
Although I dont know what it is doing?

Cheers!
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 11-22-2008, 05:06 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,491
Rep Power: 8
Fubarable is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 11-22-2008, 06:09 PM
Member
 
Join Date: Nov 2008
Posts: 8
Rep Power: 0
grahamb314 is on a distinguished road
Default
Originally Posted by Fubarable View Post
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!
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to access element in a CSV? venkatteshb New To Java 11 08-17-2008 12:37 PM
how can we get the element of by using the hashtable raj reddy Web Frameworks 1 05-06-2008 02:45 PM
Java Project Trouble: Searching one ArrayList with another ArrayList BC2210 New To Java 2 04-21-2008 12:43 PM
Max element in an Array mew New To Java 5 12-03-2007 06:26 PM
a no such element exception headlice1 New To Java 1 08-07-2007 06:36 PM


All times are GMT +2. The time now is 07:15 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org