如何在两个字符串之间进行随机连接

时间:2017-10-03 15:58:58

标签: java arrays string random

我有字符串数组名称和字符串数组公司

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)]);

     }
}

我有

Alex Puma

没关系,但我想要打印所有randomed名称和公司,例如

Alex Puma
Peter Nike
David New Balance
John Adidas

如何做到最好?

1 个答案:

答案 0 :(得分:3)

随机播放两个阵列:

Collections.shuffle(Arrays.asList(name));
Collections.shuffle(Arrays.asList(company));

然后打印每一对:

for (int i = 0; i < name.length; i++)
    System.out.println(name[i] + " " + company[i]);