Results 1 to 10 of 10
  1. #1
    grahamb314 is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default [SOLVED] ArrayList element to int

    Hi all,

    I am trying to get a certain element of my ArrayList to an int:
    Java 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!

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    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.

  3. #3
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Java 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.
    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

  4. #4
    grahamb314 is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Nicholas Jordan View Post
    Java 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:

    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!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    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:
    Java 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?

  6. #6
    grahamb314 is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default

    Quote 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:
    Java 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?

    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

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    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:

    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);
        }
        
      }
    }

  8. #8
    grahamb314 is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default

    Hi all,
    this seems to have done the trick!

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

    Cheers!

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    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.

  10. #10
    grahamb314 is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default

    Quote 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!

Similar Threads

  1. How to access element in a CSV?
    By venkatteshb in forum New To Java
    Replies: 11
    Last Post: 08-17-2008, 11:37 AM
  2. how can we get the element of by using the hashtable
    By raj reddy in forum Web Frameworks
    Replies: 1
    Last Post: 05-06-2008, 01:45 PM
  3. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  4. Max element in an Array
    By mew in forum New To Java
    Replies: 5
    Last Post: 12-03-2007, 05:26 PM
  5. a no such element exception
    By headlice1 in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •