Results 1 to 10 of 10
  1. #1
    ile4 is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default Getting an item from an array

    Hi, I'm having a small problem with an array list: I am trying to find the best way to see if it is in order without sorting it, so I'm trying to iterate through each item and check that it is < the next item.
    I have tried to use an iterator:

    Java Code:
    for (Integer node : nodes) 
    {
        Iterator it = nodes.iterator(); 
        while (it.hasNext()) 
        {
            if (node < node.next()) 
           {
    but it cannot find method next(). I don't know what I need to do to correct it, I know that a primitive array you can get the nth item by using array[n] and so maybe could have a counter and compare each item that way, but I don't know how to do it using ArrayList.

    Any ideas?

  2. #2
    charleyjoyce is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    you are already using the iterator. most probably it should be it.next(), not node.next()

  3. #3
    ile4 is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default

    when it try that it says < cannot be applied to compare the integer with an object

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ile4 View Post
    when it try that it says < cannot be applied to compare the integer with an object
    You are probably storing ints in your List. There's a mechanism called 'auto boxing' that's trying to 'help' you behind your back: it wraps the primitive int in an Integer object and stores that. The compiler can't know that but it has another mechanism: generics; you should use a List<Integer> and an Iterator<Integer> instead. Also read the tutorials on auto boxing and generics.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    ile4 is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default

    oh I see, but my list doesn't store integers, it stores nodes and each node has a integer field and that is the field I'm trying to go through and compare. Sorry, I should have explained this.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ile4 View Post
    oh I see, but my list doesn't store integers, it stores nodes and each node has a integer field and that is the field I'm trying to go through and compare. Sorry, I should have explained this.
    Then I don't understand your first code line:

    Java Code:
    for (Integer node : nodes)
    That line suggested to me that the List nodes stores Integers, sorry.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    ile4 is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default

    I see what I had done - it was actually giving me a compiler error for the for loop too but I hadn't noticed that. i've now made my array list and iterator <Integer> and instead of adding the whole node, just added the integer field that I'm trying to look at.

    I now have :

    Java Code:
     for (Integer node : nodes) {
                Iterator<Integer> it = nodes.iterator(); 
                while (it.hasNext()) {
                     if (node > it.next()) {
                         inOrder = false;
    but I've ran a debug to check it's working and what it seems to be doing is taking the first int and comparing it with all the integers after it, then taking the second int and comparing it with all the ones after that, etc. Can anyone see why/where I've gone wrong?

  8. #8
    ile4 is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default

    I have also tried

    Java Code:
    for (Integer node : nodes) {
                Iterator<Integer> it = nodes.iterator(); 
                while (it.hasNext()) {
                     if (node > it.next()) {
                         inOrder = false;
                     }
                node = it.next();
                }
            }
    but when it gets to the end node it throws a null pointer for node = it.next()

    :mad:

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ile4 View Post
    I see what I had done - it was actually giving me a compiler error for the for loop too but I hadn't noticed that. i've now made my array list and iterator <Integer> and instead of adding the whole node, just added the integer field that I'm trying to look at.

    I now have :

    Java Code:
     for (Integer node : nodes) {
                Iterator<Integer> it = nodes.iterator(); 
                while (it.hasNext()) {
                     if (node > it.next()) {
                         inOrder = false;
    but I've ran a debug to check it's working and what it seems to be doing is taking the first int and comparing it with all the integers after it, then taking the second int and comparing it with all the ones after that, etc. Can anyone see why/where I've gone wrong?
    If you want to check whether a List is ordered there's no need to use iterators; simply check if element at position i is less than element at position i+1 for all i in the range [0, n) where n is the index of the last element, so:

    Java Code:
    for (int i= 0; i < list.size()-1; i++)
       if (!yourCompare(list.get(i), list.get(i+1))) // not sorted?
          return false; // bail out
    return true; // yep, sorted
    the yourCompare(a, b) method checks whether or not a <= b, whatever that is for your type of objects.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    ile4 is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

Similar Threads

  1. Vector empty item?
    By libia in forum Java Applets
    Replies: 1
    Last Post: 12-07-2010, 11:20 AM
  2. How to get the selected item from combox
    By man4ish in forum AWT / Swing
    Replies: 5
    Last Post: 12-30-2009, 10:29 AM
  3. Get item from string array and pack with blank space
    By firewalll in forum New To Java
    Replies: 2
    Last Post: 09-02-2009, 07:38 AM
  4. [SOLVED] Item too big for JComboBox
    By nwboy74 in forum AWT / Swing
    Replies: 5
    Last Post: 02-23-2009, 08:21 PM
  5. grabbing an item from an arraylist.
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-17-2008, 08:25 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
  •