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(); [B]for(int i=0; i<size-1; i++ ){ for(int j=i; j<size-1; j++){ makeFriend(userNameList.get(i), userNameList.get(j+1)); } }[/B] } 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!