在Java中使用ArrayList的2D动态数组 [英] 2D dynamic array using ArrayList in Java

查看:523
本文介绍了在Java中使用ArrayList的2D动态数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现一个二维动态数组。行数是固定的,说ñ。但每一行的列数是不固定和等同。例如,在第一行有3个元件和第二行具有5个元素。如何使用ArrayList中做到这一点在Java中。谢谢你。

I need to implement a 2D dynamic array. The number of rows is fixed, say n. But the number of columns for each row is not fixed and equivalent. For instance, the first row has 3 elements and the second row has 5 elements. How to do this in Java using Arraylist. Thanks.

推荐答案

如何列表<名单<富>>

例如:

List<List<Foo>> list = new ArrayList<List<Foo>>();

List<Foo> row1 = new ArrayList<Foo>();
row1.add(new Foo());
row1.add(new Foo());
row1.add(new Foo());
list.add(row1);

List<Foo> row2 = new ArrayList<Foo>();
row2.add(new Foo());
row2.add(new Foo());

list.add(row2);

这篇关于在Java中使用ArrayList的2D动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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