从ArrayList打印随机字符串到控制台

时间:2016-02-09 18:43:13

标签: java random

这是我的代码:

public static void main(String[] args) {

    ArrayList<String> eMinor = new ArrayList<String>();
    eMinor.add("E");
    eMinor.add("F#");
    eMinor.add("G");
    eMinor.add("A");
    eMinor.add("B");
    eMinor.add("C");
    eMinor.add("D");

}

为了从列表中生成随机字符串并在控制台中打印,我需要添加什么?

6 个答案:

答案 0 :(得分:3)

您可以使用以下内容从列表中随机选择一个值:

System.out.println(value);

然后使用以下命令将其打印到控制台:

import java.util.ArrayList;

public class Main {

    public static ArrayList<String> clump (ArrayList<String> list)
    {
        int halfSize = list.size()/2;
        for (int i =0; i < halfSize; i++) {
             System.out.println(list);
             String newElement = list.get(i) + list.get(i+1);
             list.remove(i);
             list.remove(i);
             list.add(i, newElement);
        }
        return list;
    }

    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        list.add("6");
        list.add("7");
        list.add("8");
        list.add("9");
        list.add("10");
        list.add("11");
        list.add("12");
        list.add("13");
        System.out.println(clump(list));
    }

}

答案 1 :(得分:0)

未成年人是我最喜欢的钥匙之一,所以我会回答。你需要一个随机数发生器:

Random ran = new Random(); 
for (int i = 0, i < 10; i++) {
    int x = ran.nextInt(7)
    System.out.println(eMinor.get(x));
}

您可以使用循环生成多个音符:

If Worksheets(loc(1)).Cells(TseriesLine, 15) = Worksheets(loc(2)).Cells(TseriesLine, 15) _
   And Worksheets(loc(1)).Cells(TseriesLine, 15) = Worksheets(loc(3)).Cells(TseriesLine, 15) Then

答案 2 :(得分:0)

public static void main(String[] args) {

    ArrayList<String> eMinor = new ArrayList<String>();
    eMinor.add("E");
    eMinor.add("F#");
    eMinor.add("G");
    eMinor.add("A");
    eMinor.add("B");
    eMinor.add("C");
    eMinor.add("D");

    Random rand = new Random()
    int random = rand.nextInt(eMinor.size());
    String note = eMinor.get(random);
    System.out.println(note);
}

答案 3 :(得分:0)

你可以这样做:

int min = 3;
int range = 3;
Random random = new Random(); 
int length = min + random.nextInt(range);
String randomString = "";

for (int i = 0; i < length; ++i)
{
    randomString += eMinor.get(random.nextInt(eMinor.size() - 1));
}

System.out.println(randomString);

答案 4 :(得分:0)

你可以使用一个简单的模型,但对于复杂的应用程序来说,需要更多关注随机类;

enter code here
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Main {

 public static void main(String[] args) {

    ArrayList<String> eMinor = new ArrayList<String>();
    eMinor.add("E");
    eMinor.add("F#");
    eMinor.add("G");
    eMinor.add("A");
    eMinor.add("B");
    eMinor.add("C");
    eMinor.add("D");

    for (int f = 0; f < 3; f++) {
        System.out.printf("------------ list %s ------------\n",f);
        for (String value : generateList(eMinor)) {
            System.out.println(value);
        }
    }
 }

 private static List<String> generateList(ArrayList<String> eMinor) {

    List<String> tempList = new ArrayList<>();
    Random random = new Random();

    while (tempList.size() != eMinor.size()) {
        String value = eMinor.get(random.nextInt(eMinor.size()));
        if (!tempList.contains(value)) {
            tempList.add(value);
        }
    }

    return tempList;

 }
}

答案 5 :(得分:0)

您可以使用以下完整示例:

/ *  *由Mohammed.Kharma于2016年2月9日创建。  * /

import java.util.Random;

import java.util.ArrayList;

public class Scrambler {

public static int[] Scramble(final int key, final int elementsCount) throws NegativeArraySizeException {
    if (elementsCount < 0) {
        NegativeArraySizeException ex = new NegativeArraySizeException("scrambler elementCount");
        throw ex;
    }

    Random rand = new Random(key);
    int[] order = new int[elementsCount];
    for (int i = 0; i < elementsCount; i++) {
        order[i] = i;
    }
    int count = elementsCount;
    while (--count > 0) {

        int nextRandom = rand.nextInt(count + 1);  // 0 <= k <= count (!)
        int temp = order[count];
        order[count] = order[nextRandom];
        order[nextRandom] = temp;

    }
    return order;
}

public static void main(String[] args) {
    ArrayList<String> eMinor = new ArrayList<String>();
    eMinor.add("E");
    eMinor.add("F#");
    eMinor.add("G");
    eMinor.add("A");
    eMinor.add("B");
    eMinor.add("C");
    eMinor.add("D");

    for (int numOFRandoStrings = 0; numOFRandoStrings < 10; numOFRandoStrings++) {
        int[] randomList = Scramble(new Long(System.currentTimeMillis()).intValue() * numOFRandoStrings, 7); //numOFRandoStrings or any seed,7 means give me 7 random numbers from zero to 6 
        StringBuffer randomString = new StringBuffer();
        for (int ind = 0; ind < randomList.length; ind++) {
            randomString.append(eMinor.get(randomList[ind] ));
        }
        System.out.println("New Random String: " + randomString);
    }

}

}