打印二维 ArrayList 矩阵的值 [英] Print values of a 2D ArrayList Matrix

查看:44
本文介绍了打印二维 ArrayList 矩阵的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二维数组列表矩阵

I have a 2D Arraylist matrix like

    ArrayList[][] table = new ArrayList[10][10];
    table[0][0] = new ArrayList(); 
    table[0][1].add(10); 
    table[1][0].add(20); 
    table[1][1].add(30); 

    System.out.println("Value="+table[1][0].get()); //error line

错误发生在 System.out.println 行中.如何打印arraylist矩阵的值?谁能给我推荐一个方法??

The error occurs in the System.out.println line. how do I print the values of the arraylist matrix?? can anyone suggest me a method??

推荐答案

您似乎认为您有一个二维数字矩阵,存储为 ArrayList.那根本不是你所拥有的.相反,您有一个二维矩阵,其中每个元素都是一个 ArrayList.这意味着您的代码中确实有 3 个维度.我不认为那是你想要的.您可以通过多种方式使用已有的结构(即无需使用某些外部库)来实现二维.

You seem to think you have a 2-D matrix of numbers, stored as an ArrayList. That is not what you have at all. Instead, you have a 2-D matrix where each element is an ArrayList. That means you really have 3 dimensions represented in your code. I don't think that's what you want. There are several ways you could achieve two dimensions using the constructs you already have (i.e. without going to some external library).

数组是一个易于理解的结构,所以让我们从它开始.

The array is an easy to understand construct, so let's start with that.

Number[][] table = new Number[10][10];
table[0][0] = 0; 
table[0][1] = 10; 
table[1][0] = 20; 
table[1][1] = 30; 
System.out.println("Value="+table[1][0].get());

这段代码声明了一个 Number 类型的二维数组,然后用 10 行 10 列对其进行初始化.然后它会部分填充数字.只要您访问一个已经初始化的元素,就可以了.尝试访问尚未初始化的元素(如 table[3][4])会很糟糕.

This code declares a 2-D array of type Number, then initializes it with 10 rows and 10 columns. It then partially fills in numbers. As long as you access an element that has already been initialized, you'll be ok. Trying to access an element that hasn't yet been initialized (like table[3][4]) would be bad.

Number[][] table = { { 0, 10 }, { 20, 30 } };
System.out.println("Value=" + table[1][0]);

这和之前的一样,但都是一次性初始化的.这个特定的数组只有 2 行 2 列.

This is the same thing as before, but initialized all at once. This particular array only has 2 rows and 2 columns.

如果您想使用 ArrayList 而不是数组,那很好.您只需要意识到ArrayList 实际上将包含其他ArrayLists,每个ArrayLists 将包含Numbers.像这样:

If you want to use an ArrayList instead of an array, that's fine. You just need to realize that the ArrayList actually will contain other ArrayLists, each of which will contain Numbers. Like so:

ArrayList<ArrayList<Number>> table = new ArrayList<>();
table.add(new ArrayList<>());
table.add(new ArrayList<>());
table.get(0).add(0);
table.get(0).add(10);
table.get(1).add(20);
table.get(1).add(30);
System.out.println("Value=" + table.get(1).get(0));

在这个例子中,你首先声明一个包含 ArrayListsArrayList 包含 Numbers,并初始化外部的 ArrayList代码>.然后你创建一些内部的ArrayLists,最后给他们每个人一些Numbers.

In this example, you first declare an ArrayList that contains ArrayLists that contain Numbers, and initialize the outer ArrayList. Then you create some inner ArrayLists, and finally give each of them some Numbers.

您可以根据需要使用数组或 ArrayLists.您只需要在访问它们的元素之前正确初始化它们.如何初始化取决于你选择的数据结构.

You can use arrays or ArrayLists as you prefer. You just have to initialize them correctly before accessing their elements. How to initialize depends on the data structure you choose.

import java.util.ArrayList;
public class TwoD {
  public void example1() {
    Number[][] table = new Number[10][10];
    table[0][0] = 0;
    table[0][1] = 10;
    table[1][0] = 20;
    table[1][1] = 30;

    System.out.println("\nExample 1");
    System.out.println("Value=" + table[1][0]);
  }

  public void example2() {
    Number[][] table = { { 0, 10 }, { 20, 30 } };

    System.out.println("\nExample 2");
    System.out.println("Value=" + table[1][0]);
  }

  public void example3() {
    ArrayList<ArrayList<Number>> table = new ArrayList<>();
    table.add(new ArrayList<>());
    table.add(new ArrayList<>());
    table.get(0).add(0);
    table.get(0).add(10);
    table.get(1).add(20);
    table.get(1).add(30);

    System.out.println("\nExample 3");
    System.out.println("Value=" + table.get(1).get(0));
  }

  public static void main(String[] args) {
    TwoD me = new TwoD();
    me.example1();
    me.example2();
    me.example3();
  }
}

这篇关于打印二维 ArrayList 矩阵的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆