Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Iterating through an ArrayList

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Iterating through an ArrayList

    I have several ArrayLists with certain values stored in them and I want to check my program to check the above operators. I have tried looking at the ArrayList api and couldn't find anything that could really do what I wanted.

    Ultimately, I'm trying to code something that given user's input, it'll check
    X * Y = Z
    X / Y = Z
    X - Y = Z
    X + Y = Z
    but the problem is that I'm confused on how to make it check for all the different combinations since the .get() only iterates from 0-max size.

    Also, I know that .get() returns an Object, which I have tried to convert to an int and have it do the above but, again, it would only check the combinations when they were all in the same index.

    *edit*
    I'm also confused on how to iterate through 3 different ArrayLists
      for (int i = 0; i < list[k1].size(); i++)       //this only goes up to k1's ArrayList size
            {
            	int x = Integer.parseInt((String)list[k1].get(i));
            	if(x + y == z)                    // y and z are suppose to be like x but with list[k2] and list[k3]
            	{
            		count++;
            	}
            }

    *edit*
    I just tried using the Iterator class but I'd rather use the for loop if I could figure out a way to do the above. The below doesn't work though, it'll loop infinitely with no increasing count. It's just a general idea of what I'm trying to do.
            Iterator x = list[k1].iterator();
            Iterator y = list[k2].iterator();
            Iterator z = list[k3].iterator();
     
            int count = 0;
            int r = Integer.parseInt((String)x.next());
            int s = Integer.parseInt((String)y.next());
            int t = Integer.parseInt((String)z.next());
     
            while(x.hasNext() && y.hasNext() && z.hasNext())
            {
            	if(r + s == t)
            	{
            		count++;
            	}
            	if(r - s == t)
            	{
            		count++;
            	}
            	if(r * s == t)
            	{
            		count++;
            	}
            	if(r / s == t)
            	{
            		count++;
            	}
            }
    Last edited by Actinistia; October 29th, 2011 at 10:09 PM.


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Iterating through an ArrayList

    Hi,
    I would like to ask you to use Array List of Integer type so the declaration will be
    as follow:

    List<Integer> list= new ArrayList<Integer>();

    Then iterate it using java 5 for loop like below:
    for(Integer integerObj: list){
    //place your logic here.
    }

    bean factory
    Last edited by abani; December 18th, 2011 at 01:57 AM.

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: Iterating through an ArrayList

    Quote Originally Posted by abani View Post
    Hi,
    I would like to ask you to use Array List of Integer type so the declaration will be
    as follow:

    List<Integer> list= new ArrayList<Integer>();

    Then iterate it using java 5 for loop like below:
    for(Integer integerObj: list){
    //place your logic here.
    }
    Thanks for the response. I'm not sure how to implement this sort of thing yet though. I did figure out how to do what I wanted though. It was rather simplistic and I'm kicking myself over it. Basically I had to use a nested for loop and it was easily solved. Thanks again for the input!

Similar Threads

  1. Iterating a String
    By av8 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: July 7th, 2011, 12:37 PM
  2. While loop not iterating correctly
    By Rhyssa6 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: May 18th, 2011, 09:13 PM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. [HELP] Iterating through a jagged edged array
    By AlexBeer in forum Collections and Generics
    Replies: 2
    Last Post: April 12th, 2010, 06:33 AM
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM