Looks like this is what you want
int columns = 2;
int rows = 2;
String[][] newArray = new String[columns][rows];
newArray[0][0] = "France";
newArray[0][1] = "Blue";
newArray[1][0] = "Ireland";
newArray[1][1] = "Green";
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
System.out.println(newArray[i][j]);
}
}
Here I'll explain the code:
This declares the size of your new 2D array. In Java (and most programming languages), your first value starts at 0, so the size of this array is actually 2 rows by 2 columns
int columns = 2;
int rows = 2;
Here you are using the type String[][] to create a new 2D array with the size defined by [rows][columns].
String[][] newArray = new String[columns][rows];
You assign the values by its placement within the array.
newArray[0][0] = "France";
newArray[0][1] = "Blue";
newArray[1][0] = "Ireland";
newArray[1][1] = "Green";
Looping through i would loop through the rows, and looping through j would loop through the columns. This code loops through all rows and columns and prints out the values in each index.
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
System.out.println(newArray[i][j]);
}
}
Alternatively, assignment can be a one-liner:
String[][] newArray = {{"France", "Blue"}, {"Ireland", "Green"}};
But I don't like this way, as when you start dealing with larger sets of data (like 10,000+ points of data with many columns), hardcoding it in like this can be rough.
Answer from theGreenCabbage on Stack OverflowVideos
The Arrays class defines a couple of useful methods
- Arrays.toString - which doesn't work for nested arrays
- Arrays.deepToString - which does exactly what you want
String[][] aastr = {{"hello", "world"},{"Goodbye", "planet"}};
System.out.println(Arrays.deepToString(aastr));
Gives
[[hello, world], [Goodbye, planet]]
You just iterate twice over the elements:
StringBuffer results = new StringBuffer();
String separator = ","
float[][] values = new float[50][50];
// init values
for (int i = 0; i < values.length; ++i)
{
result.append('[');
for (int j = 0; j < values[i].length; ++j)
if (j > 0)
result.append(values[i][j]);
else
result.append(values[i][j]).append(separator);
result.append(']');
}
IMPORTANT: StringBuffer are also useful because you can chain operations, eg: buffer.append(..).append(..).append(..) since it returns a reference to self! Use synctactic sugar when available..
IMPORTANT2: since in this case you plan to append many things to the StringBuffer it's good to estimate a capacity to avoid allocating and relocating the array many times during appends, you can do it calculating the size of the multi dimensional array multiplied by the average character length of the element you plan to append.
You can use the following class which stores your data in a HashMap and is able to convert it to a two dimensional string array.
public class ArrayStructure {
private HashMap<Point, String> map = new HashMap<Point, String>();
private int maxRow = 0;
private int maxColumn = 0;
public ArrayStructure() {
}
public void add(int row, int column, String string) {
map.put(new Point(row, column), string);
maxRow = Math.max(row, maxRow);
maxColumn = Math.max(column, maxColumn);
}
public String[][] toArray() {
String[][] result = new String[maxRow + 1][maxColumn + 1];
for (int row = 0; row <= maxRow; ++row)
for (int column = 0; column <= maxColumn; ++column) {
Point p = new Point(row, column);
result[row][column] = map.containsKey(p) ? map.get(p) : "";
}
return result;
}
}
Example code
public static void main(String[] args) throws IOException {
ArrayStructure s = new ArrayStructure();
s.add(0, 0, "1");
s.add(1, 1, "4");
String[][] data = s.toArray();
for (int i = 0; i < data.length; ++i) {
for (int j = 0; j < data[i].length; ++j)
System.out.print(data[i][j] + " ");
System.out.println();
}
}
Output
1
4
You can simply initialize with a literal, empty two-dimensional array:
String[][] data = new String[][]{{}}