How to populate a two-dimensional ArrayList array in java using integers?

I need to create a 2d array with an unknown size. So I decided to go with a 2d ArrayList, the problem is that 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. to a huge 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 an array automatically update column / rows for me

Any help appreciated thanks

+5
source share
7 answers

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

Like this, for example:

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 if you want:

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 line, do

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

row,

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

:

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);
        }
    }
}
+18
List<List<Integer>> array = new ArrayList<List<Integer>>();
// add row:
array.add( new ArrayList<Integer>() );
// add a column:
array.get( array.size() -1 ).add( 1 );

:

import java.util.*;
import static java.lang.System.out;
class Load { 
    public static void main( String ... args ) { 

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

        Scanner input = new Scanner(System.in);
        out.println("Enter n:");
        int n = input.nextInt();

        out.println("Enter m:");
        int m = input.nextInt();

        out.println("Enter the values:");

        for( int i = 0 ; i < n ; i++ ) { 
            // add row:
            List<Integer> list = new ArrayList<Integer>();
            array.add( list );
            for( int j = 0 ; j < m ; j++ ) { 
                // add a column:
                // array.get( array.size() -1 ).add( 1 ); or
                list.add( input.nextInt() );
            }
        }
        out.println("Result:");
        out.println( array );

    }
}

:

C:\>java Load
Enter n:
3
Enter m:
6
Enter the values
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Result:
[[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17]]
+15

:

array.get(i1).put(i2, value);

get put , ArrayList <= . , , .

putCell(getRow(array, i1), i2, value)

getRow() , ArrayList ArrayList, putCell() , ArrayList.

0

, aioobe, Google Guava.

Table<Integer,Integer,Integer> matrix = new HashBasedTable<Integer,Integer,Integer>();
matrix.put(rowIndex,columnIndex,value);

, , , - Integer.

0

, , 3 5 ( ), :

int[][] a = new int[3][5]; 

, , - :

String dataStr = "0,1,0,1,0:0,1,1,0,1:0,0,0,1,0";

String[] rows = dataStr.split(":");

String[] cols = rows[0].split(",");

:

int[][] a = new int[rows.length][cols.length];

. , , .

0

, ints booleans ( false). (.. ), - HashSet ( , int hashCode ).

class IntPair {
   int first;
   int second;
   public boolean equals(Object o) {
      return o instanceof IntPair &&
         ((IntPair)o).first == first &&
         ((IntPair)o).second == second;
   }
   /** optimized for small numbers */
   public int hashCode() {
       return first + second * 44729;
   }
   public String toString() {
     return "(" + first + ", " + second + ")";
   }
}

, "0 1",

set.add(new IntPair(0,1));

, - HashSet , "" node 1 ". ,

class Node {
   int id;
   Set<Node> neighbours;
}

// .

" " , .

0

2D-, , ( , ) java.util.BitSet, , -:

2D-, , :

List<BitSet> bitArrays = new ArrayList<BitSet>();

: " 5- ", 4 . BitSet set() , , .

0

All Articles