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

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

问题描述

我需要实现一个二维动态数组.行数是固定的,比如 n.但是每行的列数不是固定的和等效的.例如,第一行有 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<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的二维动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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