如何填写为整数的Java二维的ArrayList? [英] How to fill a two Dimensional ArrayList in java with Integers?

查看:120
本文介绍了如何填写为整数的Java二维的ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建具有未知大小的二维数组。所以我决定去与一个二维的ArrayList问题是我不知道如何初始化这样的阵列或存储信息。

说我有以下数据

  0连接1
   2连接3
   4连接5

....等多达随机连接的大量

和我想插入

 真(1)到[0] [1]
真(1)到[2] [3],
真(1)到[4] [5]。

阵列可以自动更新柱/为我行

任何帮助AP preciated感谢


解决方案

  

我不知道如何初始化这样的阵列或存储信息。


像这样的实例:

 列表<名单,LT;整数GT;> twoDim =新的ArrayList<名单,LT;整数GT;>();twoDim.add(Arrays.asList(0,1,0,1,0));
twoDim.add(Arrays.asList(0,1,1,0,1));
twoDim.add(Arrays.asList(0,0,0,1,0));

或者类似这样的,如果你preFER:

 列表<名单,LT;整数GT;> twoDim =新的ArrayList<名单,LT;整数GT;>(){{
    加(Arrays.asList(0,1,0,1,0));
    加(Arrays.asList(0,1,1,0,1));
    加(Arrays.asList(0,0,0,1,0));
}};


要插入新行,你做

  twoDim.add(新的ArrayList<整数GT;());

和追加在一个特定的另一个元素

  twoDim.get(行)。新增(someValue中);


下面是一个更完整的例子:

 进口的java.util。*;公共类的测试{    公共静态无效的主要(字串[] args){        清单<名单,LT;整数GT;> twoDim =新的ArrayList<名单,LT;整数GT;>();        串[] inputLines = {0 1 0 1 0,0 1 1 0 1,0 0 0 1 0};        对(串线:inputLines){
            清单<整数GT;行=新的ArrayList<整数GT;();            扫描器S =新的扫描仪(线);
            而(s.hasNextInt())
                row.add(s.nextInt());            twoDim.add(行);
        }
    }
}

I have to create a 2d array with unknown size. So I have decided to go with a 2d ArrayList the problem is I'm not sure how to initialize such an array or store information.

Say I have the following data

   0 connects 1
   2 connects 3 
   4 connects 5

....etc up to a vast amount of random connections

and I want to insert

true(1) into [0][1], 
true(1) into [2][3], 
true(1) into [4][5]. 

Can the array automatically update the column/rows for me

Any help is appreciated thanks

解决方案

I'm not sure how to initialize such an array or store information.

Like this for instance:

List<List<Integer>> twoDim = new ArrayList<List<Integer>>();

twoDim.add(Arrays.asList(0, 1, 0, 1, 0));
twoDim.add(Arrays.asList(0, 1, 1, 0, 1));
twoDim.add(Arrays.asList(0, 0, 0, 1, 0));

or like this if you prefer:

List<List<Integer>> twoDim = new ArrayList<List<Integer>>() {{
    add(Arrays.asList(0, 1, 0, 1, 0));
    add(Arrays.asList(0, 1, 1, 0, 1));
    add(Arrays.asList(0, 0, 0, 1, 0));
}};


To insert a new row, you do

twoDim.add(new ArrayList<Integer>());

and to append another element on a specific row you do

twoDim.get(row).add(someValue);


Here is a more complete example:

import java.util.*;

public class Test {

    public static void main(String[] args) {

        List<List<Integer>> twoDim = new ArrayList<List<Integer>>();

        String[] inputLines = { "0 1 0 1 0", "0 1 1 0 1", "0 0 0 1 0" };

        for (String line : inputLines) {
            List<Integer> row = new ArrayList<Integer>();

            Scanner s = new Scanner(line);
            while (s.hasNextInt())
                row.add(s.nextInt());

            twoDim.add(row);
        }
    }
}

这篇关于如何填写为整数的Java二维的ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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