在 Java 中创建一个 2D ArrayList列表集合的两种不同方法

导读:本篇文章讲解 在 Java 中创建一个 2D ArrayList列表集合的两种不同方法,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

在 Java 中创建一个 2D ArrayList列表集合的两种不同方法

使用固定大小数组在 Java 中创建 2d ArrayList

通过创建 ArrayList 的 ArrayList 在 Java 中创建 2D ArrayList

 

Create 2d ArrayList in Java Using Fixed-Size Array

This first method will create an ArrayList named arraylist1 with a size of three rows and three columns. We want to insert an ArrayList of Strings in arraylist1; to do this, we will create an ArrayList object in each row and column and add data to it.

The example below shows that arraylist[0][0] is filled first, which is the first row, and the first column of arraylist1; this goes on until the ArrayList is completely filled. We are only adding data to the first row here, and the next two rows are null, making the output show null.

使用固定大小数组在 Java 中创建 2d ArrayList
第一种方法将创建一个名为arraylist1 的ArrayList,其大小为三行三列。 我们想在 arraylist1 中插入一个字符串的 ArrayList; 为此,我们将在每一行和每一列中创建一个 ArrayList 对象并向其中添加数据。

下面的例子显示arraylist[0][0]先填充,也就是arraylist1的第一行,第一列; 这一直持续到 ArrayList 被完全填满。 我们这里只在第一行添加数据,后面两行为空,使得输出显示为空。

import java.util.ArrayList;
import java.util.Arrays;

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

        ArrayList<String>[][] arraylist1 = new ArrayList[3][3];
        arraylist1[0][0] = new ArrayList<String>();
      
        arraylist1[0][0].add("String One");
        arraylist1[0][0].add("String Two");
        arraylist1[0][0].add("String Three");

        arraylist1[0][1] = new ArrayList<String>();
        arraylist1[0][1].add("String One");
        arraylist1[0][1].add("String Two");
        arraylist1[0][1].add("String Three");

        arraylist1[0][2] = new ArrayList<String >();
        arraylist1[0][2].add("String One");
        arraylist1[0][2].add("String Two");
        arraylist1[0][2].add("String Three");

        System.out.println(Arrays.deepToString(arraylist1));

    }
}

output

[[[String One, String Two, String Three], [String One, String Two, String Three], [String One, String Two, String Three]], 
 [null, null, null], 
 [null, null, null]]

Create a 2D ArrayList in Java by Creating ArrayList of ArrayList

The next method to produce a 2D list in Java is to create an ArrayList of ArrayLists; it will serve our purpose as it will be two-dimensional. To insert an innerArraylist function inside outerArrayList1, we can initialize the 2D ArrayList Java object to outerArrayList1.

The next and the last step is adding our data to the innerArraylist function and then adding it to the outerArrayList command. Note that we can add more than one ArrayLists into the outerArrayList command.

通过创建 ArrayList 的 ArrayList 在 Java 中创建 2D ArrayList
在 Java 中生成 2D 列表的下一个方法是创建 ArrayList 的 ArrayList; 它将服务于我们的目的,因为它将是二维的。 要在outerArrayList1 中插入innerArraylist 函数,我们可以将2D ArrayList Java 对象初始化为outerArrayList1。

下一步也是最后一步是将我们的数据添加到innerArraylist 函数,然后将其添加到outerArrayList 命令。 请注意,我们可以在 outerArrayList 命令中添加多个 ArrayList。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

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

        ArrayList<String> innerArraylist;

        innerArraylist = new ArrayList<String>();

        List<ArrayList<String>> outerArrayList = new ArrayList<>();

        innerArraylist.add("String One");
        innerArraylist.add("String Two");
        innerArraylist.add("String Three");


        outerArrayList.add(innerArraylist);

        System.out.println(outerArrayList.toString());

    }
}

output 

[[String One, String Two, String Three]]

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/73578.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!