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 4 of 4

Thread: Looping with an ArrayList

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Looping with an ArrayList

    			for(int i = 0; i < songList.size(); i++)
    			{
     
    				ui.displaySongs(songList.get(i), songList);
     
    			}

    SongList is an ArrayList.


    What I would like to do, is loop the variable "i", which I am using as the index for elements of an arraylist, but not loop the call to the method displaySongs. (which is in another class for user interface)

    This loop is printing my code for the amount of elements in the arrayList, when I would only like to print once for every 4 elements.

    My problem is that if I were to be adding 8 elements to the arraylist through a user interface,
    it would display the formatted statement 8 times.

    tl;dr: How can I loop the index for the get method of ArrayList, but not loop my display for the size of the arraylist.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Looping with an ArrayList

    I'm not sure I understand your question. It sounds like you want your ui to display a subset of your songlist...if that is the case, rewrite ui.displaySongs so that all you need is to pass the List and a start end index to display (or an array of values to display).

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Looping with an ArrayList

    I would only like to print once for every 4 elements.
    Try using the modulus operator with i to detect every 4th element.
    Or increment i by 4 vs by 1 (i++)

    I don't understand the rest of your requirements.

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Looping with an ArrayList

    Quote Originally Posted by EmSaint View Post
    I would only like to print once for every 4 elements.
    for(int i = 0; i < songList.size(); i += 4)
    db

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. 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
  3. looping through an ArrayList of objects with their own attributes
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: April 2nd, 2010, 06:15 AM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. [SOLVED] looping, for,while,do-while.
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: August 6th, 2009, 01:32 PM