【问题标题】:How to get 2d arraylist through loop? [duplicate]如何通过循环获取二维数组列表? [复制]
【发布时间】:2018-07-02 11:37:59
【问题描述】:

我正在通过自学学习 Java。现在我正在做一个练习。我正在尝试创建可变大小的二维数组,然后分配从 10 到 100 的随机数并将其放入每个数组中。

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

这是我的代码。

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 ")
    }
}

【问题讨论】:

  • "创建可变大小的二维数组" & "如何获取每个二维数组" - 您的代码中没有数组。
  • 你没有二维数组?
  • "Solve the ans" 是一个我不熟悉的表达方式。您是在问如何打印列表吗?或者如何遍历 List 的内容?
  • 好吧,您没有创建 2D-ArrayList。您有一个自定义“随机”类的基本一维数组列表。 2D-ArrayList 将是外部 ArrayList 的每个元素本身都是 ArrayList 的:ArrayList&lt;ArrayList&lt;?&gt;&gt;
  • 与此要求类似的是How to create a Multidimensional ArrayList in Java?,其中包含示例。

标签: java object arraylist


【解决方案1】:

你错过了一些事情来完成这项工作。

  1. 在 Random 类中添加一个 toString() 方法。对象中的 toString() 方法用于将对象更改为局部变量的字符串表示形式(在“随机”情况下,您希望返回带有 ID 和 randomNum 的字符串,请参见下面的代码)。

  2. String result; 需要分配一个初始值才能使用“+=”。改成String result = "";

  3. 现在我们有了一个“toString()”方法,您可以使用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);
    
        }
    }
    

【讨论】:

  • 感谢您抽出宝贵时间先解决这个问题!我会尽力理解你在这里写的每一个代码!
【解决方案2】:

当您可以使用您创建的 Random#getID 方法时,我不明白为什么需要二维数组:

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

但这里有一个使用Map 创建二维数组列表的方法:

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

【讨论】:

  • 感谢您帮助写下如何制作二维数组列表!
  • @Let'sYo 答案已更新。
  • 再次感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2020-12-08
  • 2013-12-23
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 2020-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多