如何通过循环获得2d arraylist?

时间:2018-01-23 15:28:51

标签: java object arraylist

我正在通过自我阅读来学习Java。现在我正在做运动。我正在尝试创建可变大小的2D数组,然后分配从10到100的随机数并将其放入每个数组中。

我遇到的问题是不知道如何将每个2D数组输出并将其放入字符串中,然后在完成创建变量对象后通过对话框显示它。

这是我的代码。

import java.security.SecureRandom;
import javax.swing.JOptionPane;
import java.util.ArrayList;

public class Random {
    public int randomNum;
    public String ID;

    public Random(String ID, int initialValue) {
        SecureRandom randomNumbers = new SecureRandom();
        this.ID = ID;
        this.randomNum = initialValue;
        int randomValue = randomNumbers.nextInt(99) + 1;
        randomNum = randomValue;
    }

    public int getRandomNum() {
        return randomNum;
    }

    public String getID() {
        return ID;
    }
}

class RandomText {
    public static void main(String[] args) {
        int ans = Integer.parseInt(JOptionPane.showInputDialog("How many random number you want to show?"));

        ArrayList < Random > randomNum = new ArrayList < Random > ();
        for (int i = 0; i < ans; i++) {
            randomNum.add(new Random("ID " + Integer.toString(i), 0));
        }
        String result;
        for (int i = 0; i < ans; i++) {
            result = result + ?????? +"\n";
        }
        JOptionPane.showMessageDialog(null, result ")
    }
}

2 个答案:

答案 0 :(得分:2)

你错过了一些使这项工作成功的事情。

  1. 在Random类中添加toString()方法。对象中的toString()方法用于将对象更改为局部变量的字符串表示形式(In&#34; Random&#34; case,您要返回带有ID和randomNum的字符串,请参阅下面的代码)。

  2. String result;需要分配初始值才能使用&#39; + =&#39;。将其更改为String result = "";

  3. 现在我们有一个&#34; toString()&#34;方法,您可以使用result = result + randomNum.get(i).toString();

    将其附加到结果中
    import java.security.SecureRandom;
    import javax.swing.JOptionPane;
    import java.util.ArrayList;
    
    public class Random {
        public int randomNum;
        public String ID;
    
        public Random(String ID,int initialValue){
            SecureRandom randomNumbers = new SecureRandom();
            this.ID = ID;
            this.randomNum = initialValue;
            int randomValue = randomNumbers.nextInt(99)+1;
            randomNum = randomValue;
        }
    
        public int getRandomNum(){
            return randomNum;
        }
    
        public String getID(){
            return ID;
        }
    
        public String toString(){
            return ID + ": " + randomNum;
        }
    }
    
    class RandomText{
        public static void main(String[] args) {
            int ans = Integer.parseInt(JOptionPane.showInputDialog
                    ("How many random number you want to show?"));
    
            ArrayList<Random> randomNum = new ArrayList<Random>();
            for (int i = 0; i < ans; i++) {
                randomNum.add(new Random("ID " + Integer.toString(i),0));
            }
            String result = "";
            for (int i = 0; i < ans; i++) {
                result = result + randomNum.get(i).toString() + "\n";
            }
            JOptionPane.showMessageDialog(null, result);
    
        }
    }
    

答案 1 :(得分:1)

当您只使用您创建的Random#getID方法时,我不明白为什么需要2D数组:

String result;
for (Random random : randomNum) {
    result += random.getID() " : " + random.getRandomNum() + "\n";
}

但这是一个使用Map创建2D数组列表的方法:

    Map<String, List<Integer>> idNums = new HashMap<String, List<Integer>>();
    randomNum.stream().forEach(r -> {
        if (idNums.get(r.getID()) != null) {
            idNums.get(r.getID()).add(r.getRandomNum());
        } else {
            idNums.put(r.getID(), Arrays.asList(r.getRandomNum()));
        }
    });

    ArrayList<List<Integer>> ids = new ArrayList<List<Integer>>();
    idNums.entrySet().forEach(e -> ids.add(e.getValue()));