Since the number of columns is a constant, you can just have an List of int[].
import java.util.*;
//...
List<int[]> rowList = new ArrayList<int[]>();
rowList.add(new int[] { 1, 2, 3 });
rowList.add(new int[] { 4, 5, 6 });
rowList.add(new int[] { 7, 8 });
for (int[] row : rowList) {
System.out.println("Row = " + Arrays.toString(row));
} // prints:
// Row = [1, 2, 3]
// Row = [4, 5, 6]
// Row = [7, 8]
System.out.println(rowList.get(1)[1]); // prints "5"
Since it's backed by a List, the number of rows can grow and shrink dynamically. Each row is backed by an int[], which is static, but you said that the number of columns is fixed, so this is not a problem.
Since the number of columns is a constant, you can just have an List of int[].
import java.util.*;
//...
List<int[]> rowList = new ArrayList<int[]>();
rowList.add(new int[] { 1, 2, 3 });
rowList.add(new int[] { 4, 5, 6 });
rowList.add(new int[] { 7, 8 });
for (int[] row : rowList) {
System.out.println("Row = " + Arrays.toString(row));
} // prints:
// Row = [1, 2, 3]
// Row = [4, 5, 6]
// Row = [7, 8]
System.out.println(rowList.get(1)[1]); // prints "5"
Since it's backed by a List, the number of rows can grow and shrink dynamically. Each row is backed by an int[], which is static, but you said that the number of columns is fixed, so this is not a problem.
There are no multi-dimensional arrays in Java, there are, however, arrays of arrays.
Just make an array of however large you want, then for each element make another array however large you want that one to be.
int array[][];
array = new int[10][];
array[0] = new int[9];
array[1] = new int[8];
array[2] = new int[7];
array[3] = new int[6];
array[4] = new int[5];
array[5] = new int[4];
array[6] = new int[3];
array[7] = new int[2];
array[8] = new int[1];
array[9] = new int[0];
Alternatively:
List<Integer>[] array;
array = new List<Integer>[10];
// of you can do "new ArrayList<Integer>(the desired size);" for all of the following
array[0] = new ArrayList<Integer>();
array[1] = new ArrayList<Integer>();
array[2] = new ArrayList<Integer>();
array[3] = new ArrayList<Integer>();
array[4] = new ArrayList<Integer>();
array[5] = new ArrayList<Integer>();
array[6] = new ArrayList<Integer>();
array[7] = new ArrayList<Integer>();
array[8] = new ArrayList<Integer>();
array[9] = new ArrayList<Integer>();
java - Dynamic two dimensional array - Stack Overflow
java - How to dynamically increase size of a 2D array - Stack Overflow
list - How to create Dynamic Two Dimensional Array [JAVA] - Stack Overflow
2D dynamic array using ArrayList in Java - Stack Overflow
Videos
This isn't possible, arrays are always static in length.
Try using a list of lists.
LinkedList<LinkedList<String>> list = new LinkedList<LinkedList<String>>();
list.add(new LinkedList<String>()); // [[]]
list.get(0).add("hello"); // [["hello"]]
list.get(0).add("world"); // [["hello", "world"]]
list.add(new LinkedList<String>()); // [["hello", "world"], []]
list.get(1).add("bonjour"); // [["hello", "world"], ["bonjour"]]
The list can use any class instead of String.
To loop through the lists, you'd need to do something like the following:
for(LinkedList<String> subList : list){
for(String str : subList){
...
}
}
You'll want to use List or ArrayList it allows for 2 dimensional arrays with different data types and you can add/remove rows as needed.
see 2 dimensional array list
Note that new int[2][4] is an array of int[]; the int[] arrays in the int[][] array are all initially the same length, but there's no requirement that they remain the same length. Any element of the int[][] array can be reassigned an int[] with a different length without affecting the other elements at all. The concept of "rows" and "columns" is a higher-level idea that is not supported by Java arrays.
Using an ArrayList as other answers suggest isn't going to change this. Furthermore, Depending on how you use ArrayList, you may end up with considerable overhead due to autoboxing of int values as Integer objects.
If you want to preserve the rectangular shape of your data, I suggest that you define a Matrix class that keeps all the dimensions consistent. (Or, perhaps better, linearizes the two-dimensional array into a one-dimensional array and does the appropriate subscripting calculations using internally stored row and column sizes. Or, perhaps best, use a well-written matrix library such as JAMA or a primitive collections library like Trove.)
EDIT Here's the start of a simple matrix class that uses a linear storage scheme internally and allows matrix resizing. The data are stored in row-major order and indexing is based at 0.
public class IntMatrix {
private int rows;
private int cols;
private int[] data;
/**
* Allocate a matrix with the indicated initial dimensions.
* @param cols The column (horizontal or x) dimension for the matrix
* @param rows The row (vertical or y) dimension for the matrix
*/
public IntMatrix(int cols, int rows) {
this.rows = rows;
this.cols = cols;
data = new int[cols * rows];
}
/**
* Calculates the index of the indicated row and column for
* a matrix with the indicated width. This uses row-major ordering
* of the matrix elements.
* <p>
* Note that this is a static method so that it can be used independent
* of any particular data instance.
* @param col The column index of the desired element
* @param row The row index of the desired element
* @param width The width of the matrix
*/
private static int getIndex(int col, int row, int width) {
return row * width + col;
}
public int get(int col, int row) {
return data[getIndex(col, row, cols)];
}
public void set(int col, int row, int value) {
data[getIndex(col, row, cols)] = value;
}
/**
* Resizes the matrix. The values in the current matrix are placed
* at the top-left corner of the new matrix. In each dimension, if
* the new size is smaller than the current size, the data are
* truncated; if the new size is larger, the remainder of the values
* are set to 0.
* @param cols The new column (horizontal) dimension for the matrix
* @param rows The new row (vertical) dimension for the matrix
*/
public void resize(int cols, int rows) {
int [] newData = new int[cols * rows];
int colsToCopy = Math.min(cols, this.cols);
int rowsToCopy = Math.min(rows, this.rows);
for (int i = 0; i < rowsToCopy; ++i) {
int oldRowStart = getIndex(0, i, this.cols);
int newRowStart = getIndex(0, i, cols);
System.arraycopy(data, oldRowStart, newData, newRowStart,
colsToCopy
);
}
data = newData;
}
. . .
}
ArrayList documentation, and Examples. Enjoy!
Specifically:
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1); // Add 1 to the list.
It may be more appropriate to use a Map<Double, List<Double>>. Having the value of the Map as a List<Double> will allow you to expand the list as opposed to an array which has a fixed size.
public static void main(String[] args) throws CloneNotSupportedException {
Map<Double, List<Double>> myMap = create(1, 3);
}
public static Map<Double, List<Double>> create(double row, double column) {
Map<Double, List<Double>> doubleMap = new HashMap<Double, List<Double>>();
for (double x = 0; x < row; x++) {
for (double y = 0; y < column; y++) {
doubleMap.put(x, new ArrayList<Double>());
}
}
return doubleMap;
}
Try to use
Map<String, ArrayList> lastSecArray = new HashMap<>();
ArrayList value = new ArrayList();
value.add(0);
value.add(1);
value.add(2);
lastSecArray.put("0", value);
so you can operate with
lastSecArray.size()
or
lastSecArray.put(...)
or array in array
How about List<List<Foo>> ?
For Example:
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);
ArrayList<ArrayList<SomeObject>> twodlist = new ArrayList<ArrayList<SomeObject>>();
ArrayList<SomeObject> row = new ArrayList<SomeObject>();
row.add(new SomeObject(/* whatever */));
// etc
twodlist.add(row);
row = new ArrayList<SomeObject>();
// etc