Reputation: 383
I have string array name and string array company
public class Main {
public static void main(String[] args) {
String[] name = {"Alex", "John", "David", "Peter"};
String[] company = {"Adidas", "Nike", "New Balance", "Puma"};
Random random = new Random();
System.out.println(name[random.nextInt(name.length)] + " " + company[random.nextInt(company.length)]);
}
}
I've got
Alex Puma
That's OK, but I want print all randomed names and companies, such as
Alex Puma
Peter Nike
David New Balance
John Adidas
How to do it best way?
Upvotes: 1
Views: 164
Reputation: 129507
Shuffle both arrays:
Collections.shuffle(Arrays.asList(name));
Collections.shuffle(Arrays.asList(company));
Then print each pair:
for (int i = 0; i < name.length; i++)
System.out.println(name[i] + " " + company[i]);
Upvotes: 2