Results 1 to 8 of 8
  1. #1
    newjava is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Question for loop in java

    Hi,
    I am new here and beginner for java.
    I wrote a simple List program with for loop.

    public class UserDemo {

    List<String> userNameList = new ArrayList<String>();

    public UserDemo (int number, String prefix){
    for(int i =1; i <= number; i++){
    addUser(prefix +"."+ i);
    }
    System.out.println("User :" +userNameList );

    int size = userNameList.size();
    for(int i=0; i<size-1; i++ ){
    for(int j=i; j<size-1; j++){
    makeFriend(userNameList.get(i),
    userNameList.get(j+1));
    }
    }

    }

    public void addUser (String username){
    userNameList.add(username);
    }

    public void makeFriend (String currentUsername, String
    previousUsername){
    System.out.println(currentUsername + " is friend of-> "
    +previousUsername);
    }

    public static void main(String[] args) {
    UserDemo au = new UserDemo(5,"newjava");
    }
    }

    After running this code i m getting output in this form

    User :[newjava.1, newjava.2, newjava.3, newjava.4, newjava.5]
    newjava.1 is friend of-> newjava.2
    newjava.1 is friend of-> newjava.3
    newjava.1 is friend of-> newjava.4
    newjava.1 is friend of-> newjava.5
    newjava.2 is friend of-> newjava.3
    newjava.2 is friend of-> newjava.4
    newjava.2 is friend of-> newjava.5
    newjava.3 is friend of-> newjava.4
    newjava.3 is friend of-> newjava.5
    newjava.4 is friend of-> newjava.5


    but i want output in this form

    User :[newjava.1, newjava.2, newjava.3, newjava.4, newjava.5]
    newjava.1 is friend of-> newjava.2
    newjava.1 is friend of-> newjava.3
    newjava.1 is friend of-> newjava.4
    newjava.1 is friend of-> newjava.5

    newjava.2 is friend of-> newjava.1
    newjava.2 is friend of-> newjava.3
    newjava.2 is friend of-> newjava.4
    newjava.2 is friend of-> newjava.5

    newjava.3 is friend of-> newjava.1
    newjava.3 is friend of-> newjava.2
    newjava.3 is friend of-> newjava.4
    newjava.3 is friend of-> newjava.5

    newjava.4 is friend of-> newjava.1
    newjava.4 is friend of-> newjava.2
    newjava.4 is friend of-> newjava.3
    newjava.4 is friend of-> newjava.5

    newjava.5 is friend of-> newjava.1
    newjava.5 is friend of-> newjava.2
    newjava.5 is friend of-> newjava.3
    newjava.5 is friend of-> newjava.4

    Please help me that what to write within for loop, so it will give output
    as mentioned.

    Thanks in advance!

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Then don't use j=i+1, and don't use size - 1, and don't use get(j+1). Use j=0, size, if (j == i) continue, and get(j).
    Last edited by masijade; 12-16-2009 at 09:34 AM.

  3. #3
    Krooger is offline Member
    Join Date
    Nov 2008
    Posts
    43
    Rep Power
    0

    Default

    Your going to want to try something like:

    Java Code:
       int size = userNameList.size();
       for(int i=0; i<size-1; i++ ) {
          for(int j=0; j<size-1; j++){ 
             if (i!=j)
                makeFriend(userNameList.get(i), userNameList.get(j));
          }
       System.out.println();
       }
    Also try to use code blocks, makes reading easier.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by Krooger View Post
    Java Code:
       int size = userNameList.size();
       for(int i=0; i<size-1; i++ ) {
          for(int j=0; j<size-1; j++){ 
             if (i!=j)
                makeFriend(userNameList.get(i), userNameList.get(j));
          }
       System.out.println();
       }
    What about the last elements in the list?

    And there is no reason to do it for him.

  5. #5
    Krooger is offline Member
    Join Date
    Nov 2008
    Posts
    43
    Rep Power
    0

    Default

    Oh yes, use size NOT size-1, good call... Sleeeepy

  6. #6
    newjava is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default

    Ahhaa!! Its working now... Thank u very much Masijade & Krooger,
    From next time i'll take care to use Code block...

  7. #7
    Join Date
    Dec 2009
    Location
    delhi india
    Posts
    3
    Rep Power
    0

    Default

    U can also use below [CODE]

    for (int i = 0; i < size; i++) {
    for (int j = 0; j < size; j++) {
    if (i != j) {
    makeFriend(userNameList.get(i), userNameList.get(j));
    }
    }
    }

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

Similar Threads

  1. Java: While loop help
    By alfredoooddd in forum New To Java
    Replies: 3
    Last Post: 11-05-2008, 04:48 AM
  2. Help with loop in java
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 08:36 AM
  3. Help, loop with java
    By cachi in forum New To Java
    Replies: 5
    Last Post: 08-01-2007, 07:03 AM
  4. Enhanced For loop In Java
    By goldhouse in forum Advanced Java
    Replies: 1
    Last Post: 05-06-2007, 05:26 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
  •