如何将多个ArrayLists连接成一个?

时间:2016-02-13 20:24:06

标签: java arrays arraylist

我必须编写一个可以滚动10个骰子的代码" N"次数。我必须使用" *"来制作结果的直方图(10到60)。显示出现的钟形曲线。如何组合所有不同的ArrayLists,使我的代码看起来更干净?是否有更简单的方法可以一起完成这一切?

我得到了正确的结果;我只需要一种更清晰或更有效的方式来显示我的代码。

public class TenDice {
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        System.out.println("Enter number of trials: ");
        int roll = scan.nextInt();


        ArrayList<String> results10 = new ArrayList<>();
        ArrayList<String> results11 = new ArrayList<>();
        ArrayList<String> results12 = new ArrayList<>();
        ArrayList<String> results13 = new ArrayList<>();
        ArrayList<String> results14 = new ArrayList<>();
        ArrayList<String> results15 = new ArrayList<>();
        ArrayList<String> results16 = new ArrayList<>();
        ArrayList<String> results17 = new ArrayList<>();
        ArrayList<String> results18 = new ArrayList<>();
        ArrayList<String> results19 = new ArrayList<>();
        ArrayList<String> results20 = new ArrayList<>();
        ArrayList<String> results21 = new ArrayList<>();
        ArrayList<String> results22 = new ArrayList<>();
        ArrayList<String> results23 = new ArrayList<>();
        ArrayList<String> results24 = new ArrayList<>();
        ArrayList<String> results25 = new ArrayList<>();
        ArrayList<String> results26 = new ArrayList<>();
        ArrayList<String> results27 = new ArrayList<>();
        ArrayList<String> results28 = new ArrayList<>();
        ArrayList<String> results29 = new ArrayList<>();
        ArrayList<String> results30 = new ArrayList<>();
        ArrayList<String> results31 = new ArrayList<>();
        ArrayList<String> results32 = new ArrayList<>();
        ArrayList<String> results33 = new ArrayList<>();
        ArrayList<String> results34 = new ArrayList<>();
        ArrayList<String> results35 = new ArrayList<>();
        ArrayList<String> results36 = new ArrayList<>();
        ArrayList<String> results37 = new ArrayList<>();
        ArrayList<String> results38 = new ArrayList<>();
        ArrayList<String> results39 = new ArrayList<>();
        ArrayList<String> results40 = new ArrayList<>();
        ArrayList<String> results41 = new ArrayList<>();
        ArrayList<String> results42 = new ArrayList<>();
        ArrayList<String> results43 = new ArrayList<>();
        ArrayList<String> results44 = new ArrayList<>();
        ArrayList<String> results45 = new ArrayList<>();
        ArrayList<String> results46 = new ArrayList<>();
        ArrayList<String> results47 = new ArrayList<>();
        ArrayList<String> results48 = new ArrayList<>();
        ArrayList<String> results49 = new ArrayList<>();
        ArrayList<String> results50 = new ArrayList<>();
        ArrayList<String> results51 = new ArrayList<>();
        ArrayList<String> results52 = new ArrayList<>();
        ArrayList<String> results53 = new ArrayList<>();
        ArrayList<String> results54 = new ArrayList<>();
        ArrayList<String> results55 = new ArrayList<>();
        ArrayList<String> results56 = new ArrayList<>();
        ArrayList<String> results57 = new ArrayList<>();
        ArrayList<String> results58 = new ArrayList<>();
        ArrayList<String> results59 = new ArrayList<>();
        ArrayList<String> results60 = new ArrayList<>();

        for (int i = 0; i < roll; i++) {

            double dice1 = Math.random();
            dice1 = (int) (dice1 * 6);

            double dice2 = Math.random();
            dice2 = (int) (dice2 * 6);

            double dice3 = Math.random();
            dice3 = (int) (dice3 * 6);

            double dice4 = Math.random();
            dice4 = (int) (dice4 * 6);

            double dice5 = Math.random();
            dice5 = (int) (dice5 * 6);

            double dice6 = Math.random();
            dice6 = (int) (dice6 * 6);

            double dice7 = Math.random();
            dice7 = (int) (dice7 * 6);

            double dice8 = Math.random();
            dice8 = (int) (dice8 * 6);

            double dice9 = Math.random();
            dice9 = (int) (dice9 * 6);

            double dice10 = Math.random();
            dice10 = (int) (dice10 * 6);

            double total = dice1 + dice2 + dice3 + dice4 + dice5 +
                    dice6 + dice7 + dice8 + dice9 + dice10;

            if (total == 10) {
                results10.add("*");
            }
            if (total == 11) {
                results11.add("*");
            }
            if (total == 12) {
                results12.add("*");
            }
            if (total == 13) {
                results13.add("*");
            }
            if (total == 14) {
                results14.add("*");
            }
            if (total == 15) {
                results15.add("*");
            }
            if (total == 16) {
                results16.add("*");
            }
            if (total == 17) {
                results17.add("*");
            }
            if (total == 18) {
                results18.add("*");
            }
            if (total == 19) {
                results19.add("*");
            }
            if (total == 20) {
                results20.add("*");
            }
            if (total == 21) {
                results21.add("*");
            }
            if (total == 22) {
                results22.add("*");
            }
            if (total == 23) {
                results23.add("*");
            }
            if (total == 24) {
                results24.add("*");
            }
            if (total == 25) {
                results25.add("*");
            }
            if (total == 26) {
                results26.add("*");
            }
            if (total == 27) {
                results27.add("*");
            }
            if (total == 28) {
                results28.add("*");
            }
            if (total == 29) {
                results29.add("*");
            }
            if (total == 30) {
                results30.add("*");
            }
            if (total == 31) {
                results31.add("*");
            }
            if (total == 32) {
                results32.add("*");
            }
            if (total == 33) {
                results33.add("*");
            }
            if (total == 34) {
                results34.add("*");
            }
            if (total == 35) {
                results35.add("*");
            }
            if (total == 36) {
                results36.add("*");
            }
            if (total == 37) {
                results37.add("*");
            }
            if (total == 38) {
                results38.add("*");
            }
            if (total == 39) {
                results39.add("*");
            }
            if (total == 40) {
                results40.add("*");
            }
            if (total == 41) {
                results41.add("*");
            }
            if (total == 42) {
                results42.add("*");
            }
            if (total == 43) {
                results43.add("*");
            }
            if (total == 44) {
                results44.add("*");
            }
            if (total == 45) {
                results45.add("*");
            }
            if (total == 46) {
                results46.add("*");
            }
            if (total == 47) {
                results47.add("*");
            }
            if (total == 48) {
                results48.add("*");
            }
            if (total == 49) {
                results49.add("*");
            }
            if (total == 50) {
                results50.add("*");
            }
            if (total == 51) {
                results51.add("*");
            }
            if (total == 52) {
                results52.add("*");
            }
            if (total == 53) {
                results53.add("*");
            }
            if (total == 54) {
                results54.add("*");
            }
            if (total == 55) {
                results55.add("*");
            }
            if (total == 56) {
                results56.add("*");
            }
            if (total == 57) {
                results57.add("*");
            }
            if (total == 58) {
                results58.add("*");
            }
            if (total == 59) {
                results59.add("*");
            }
            if (total == 60) {
                results60.add("*");
            }


        }
        System.out.println("10: " + results10 + "\n" + "11: " + results11 + "\n" + "12: " + results12 + "\n" + "13: " + results13 + "\n" +
                "14: " + results14 + "\n" + "15: " + results15 + "\n" + "16: " + results16 + "\n" + "17: " + results17 + "\n" +
                "18: " + results18 + "\n" + "19: " + results19 + "\n" + "20: " + results20 + "\n" + "21: " + results21 + "\n" +
                "22: " + results22 + "\n" + "23: " + results23 + "\n" + "24: " + results24 + "\n" + "25: " + results25 + "\n" +
                "26: " + results26 + "\n" + "27: " + results27 + "\n" + "28: " + results28 + "\n" + "29: " + results29 + "\n" +
                "30: " + results30 + "\n" + "31: " + results31 + "\n" + "32: " + results32 + "\n" + "33: " + results33 + "\n" +
                "34: " + results34 + "\n" + "35: " + results35 + "\n" + "36: " + results36 + "\n" + "37: " + results37 + "\n" +
                "38: " + results38 + "\n" + "39: " + results39 + "\n" + "40: " + results40 + "\n" + "41: " + results41 + "\n" +
                "42: " + results42 + "\n" + "43: " + results43 + "\n" + "44: " + results44 + "\n" + "45: " + results45 + "\n" +
                "46: " + results46 + "\n" + "47: " + results47 + "\n" + "48: " + results48 + "\n" + "49: " + results49 + "\n" +
                "50: " + results50 + "\n" + "51: " + results51 + "\n" + "52: " + results52 + "\n" + "53: " + results53 + "\n" +
                "54: " + results54 + "\n" + "55: " + results55 + "\n" + "56: " + results56 + "\n" + "57: " + results57 + "\n" +
                "58: " + results58 + "\n" + "59: " + results59 + "\n" + "60: " + results60);
    }
}

3 个答案:

答案 0 :(得分:1)

是时候在Java 8中使用Stream API了。

要获得10个骰子的总和,从1到6,你可以做到

Random rnd = new Random();

long sum = IntStream.range(0, 10).map(i -> rand.nextInt(6) + 1).sum();

你可以制作很多卷并分发这些卷。

Map<Long, Long> rollCount = 
       IntStream.range(0, roll)
                .map(r -> rnd.ints(10, 1, 7).sum())
                .collect(Collectors.groupingBy(s -> s, Collectors.counting()));

所以这将滚动10个骰子,roll次,并计算每个结果。

您可以使用

打印发行版
rollCount.entrySet().stream()
         .sorted(e -> e.getKey())
         .forEach(e -> System.out.println(e + ": " + 
               IntStream.range(0, 10).mapToObj(i -> "*").collect(Collectors.joining("")));

让我们说我们不能使用Java 8.我们可以使用循环和更简单的代码。

 List<String> countList = new ArrayList<>();
 for(int i = 0; i <= 6 * 10; i++)
     countList.add(""); // start with nothing.
 Random rnd = new Random();
 for (int r = 0; r < roll; r++) {
     int sum = 0;
     for (int i = 0; i < 10; i++)
         sum += rnd.randInt(6) + 1;
     String stars = countList.get(sum);
     stars += "*";
     countList.set(sum, stars);
 }

答案 1 :(得分:0)

为什么不在每个试验中使用嵌套的arraylist?

public class TenDice {

    private static String strJoin(String[] lst, String delimeter) {
        StringBuilder sb = new StringBuilder();

        for (String s : lst) {
            if (s != null) {
                sb.append(s).append(delimeter);
            }
        }

        sb.setLength(sb.length() - delimeter.length());
        return sb.toString();
    }

    public static class HistogramBar {
        private String[] data;
        private int top;
        private int size;

        public HistogramBar(int size) {
            data = new String[size];
        }

        public void add(String s) {
            data[top++] = s;
            size++;
        }

        public String[] getData() {
            return data;
        }

        public int getSize() {
            return size;
        }
    }

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        System.out.println("Enter number of trials: ");
        int trials = scan.nextInt();
        int numberDice = 10;
        int sidesPerDice = 6;

        HistogramBar[] results = getDiceTrialHistogram(numberDice, sidesPerDice, trials);
        System.out.println(results.length);

        for (int i = 0; i < results.length; i++) {
            int sum = i + 10;
            int length = results[i].getSize();
            System.out.printf("%d: %d\n", sum, length);
        }

        System.out.println("\nHistogram");

        for (HistogramBar histBar : results) {
            System.out.printf("|%s\n", strJoin(histBar.getData(), ""));
        }
    }

    private static HistogramBar[] getDiceTrialHistogram(int numberDice, int sidesPerDice, int trials) {
        int totalSum = numberDice * sidesPerDice;
        int size = totalSum - numberDice + 1;

        HistogramBar[] results = new HistogramBar[size];
        for (int i = 0; i < size; i++) {
            results[i] = new HistogramBar(trials);
        }

        Random r = new Random();

        for (int i = 0; i < trials; i++) {
            int total = 0;

            for (int rolls = 0; rolls < numberDice; rolls++) {
                total += r.nextInt(6) + 1;
            }

            HistogramBar totalList = results[total - numberDice + 1];
            totalList.add("*");
        }

        return results;
    }

}

<强>输出

Enter number of trials: 
1000
10: 0
11: 0
12: 0
13: 0
14: 0
15: 0
16: 0
17: 0
18: 0
19: 0
20: 1
21: 1
22: 5
23: 7
24: 7
25: 20
26: 28
27: 24
28: 47
29: 40
30: 48
31: 43
32: 69
33: 58
34: 64
35: 77
36: 73
37: 62
38: 85
39: 33
40: 51
41: 36
42: 33
43: 19
44: 29
45: 10
46: 11
47: 7
48: 8
49: 4
50: 0
51: 0
52: 0
53: 0
54: 0
55: 0
56: 0
57: 0
58: 0
59: 0
1000
Histogram
|
|
|
|
|
|
|
|
|
|
|*
|*
|***
|********
|************
|***********************
|*********************
|**************************
|**************************
|*********************************************
|***********************************************
|**********************************************************
|**********************************************************
|**************************************************************************
|*******************************************************************************************
|******************************************************************
|*******************************************************************
|*****************************************************
|********************************************************************
|**************************************************************
|**************************************
|********************************
|**************************************
|***************************
|******************
|***********
|******
|**********
|*****
|****
|*
|
|
|
|
|
|
|
|
|

答案 2 :(得分:0)

在Java 8中

int numberDice = 10;
int sidesPerDice = 6;
int roll = 1000;
Random random = new Random();

int[] freq = IntStream.range(0, roll)
    .map(r -> random.ints(numberDice, 1, sidesPerDice + 1).sum())
    .collect(() -> new int[numberDice * sidesPerDice + 1], (a, e) -> ++a[e],
        (a, b) -> { for (int i = 0; i < a.length; ++i) a[i] += b[i];});

IntStream.range(numberDice, freq.length)
    .forEach(i -> System.out.printf("%4d:%4d:%s%n", i, freq[i],
        String.format("%" + (freq[i] + 1) + "s", "").substring(1).replaceAll(" ", "*")));

结果

  10:   0:
  11:   0:
  12:   0:
  13:   0:
  14:   0:
  15:   0:
  16:   0:
  17:   0:
  18:   0:
  19:   0:
  20:   1:*
  21:   5:*****
  22:   6:******
  23:   5:*****
  24:  10:**********
  25:  18:******************
  26:  23:***********************
  27:  31:*******************************
  28:  40:****************************************
  29:  41:*****************************************
  30:  45:*********************************************
  31:  51:***************************************************
  32:  54:******************************************************
  33:  62:**************************************************************
  34:  72:************************************************************************
  35:  76:****************************************************************************
  36:  71:***********************************************************************
  37:  67:*******************************************************************
  38:  71:***********************************************************************
  39:  56:********************************************************
  40:  46:**********************************************
  41:  36:************************************
  42:  31:*******************************
  43:  24:************************
  44:  23:***********************
  45:  15:***************
  46:   5:*****
  47:   5:*****
  48:   5:*****
  49:   3:***
  50:   1:*
  51:   1:*
  52:   0:
  53:   0:
  54:   0:
  55:   0:
  56:   0:
  57:   0:
  58:   0:
  59:   0:
  60:   0: