How to make an array of arrays in Java
Like this:
String[][] arrays = { array1, array2, array3, array4, array5 };
or
String[][] arrays = new String[][] { array1, array2, array3, array4, array5 };
(The latter syntax can be used in assignments other than at the point of the variable declaration, whereas the shorter syntax only works with declarations.)
How to make an array of integer arrays in Java?
private int[][] array = new int[10][10];
And you wrote something like this :
ArrayList<ArrayList<Integer>>
This is not an array, but list. You might read this : https://www.geeksforgeeks.org/array-vs-arraylist-in-java/
How do I allocate an array of arrays in Java?
You can declare a two-dimensional array and allocate the first dimensional
int [][] a = new int[n][];
And then, inside a loop, you can allocate the second one
for (int i=0; i<n; i++)
a[i] = new int[necessary_length];
But if you know the size in advance you obviously can declare it in the beginning
int [][] a = new int[n][n];
How to create an array that holds arrays in java
You have many misconceptions on Java. Let me demonstrate with line numbers.
private MazeStatus[][] stringToMaze(String sMaze) {
String[] splitString = sMaze.split("\n"); // line 1
char[][] array = new char[1][]; // line 2
for (int x = 0; x < splitString.length; x++) { // line 3
array[1][x] = splitString[x].toCharArray(); // line 4
}
return null; // TO DO
}
I am assuming you want to take an input and convert that to an object of MazeStatus (which appears to be missing with your question).
In Line:
1 - you get each row of the maze, which is represented by characters.
2 - You declare a 2D char array, but only initialize one of the dimensions. If you notice, you will find that the other dimension of the array is still unknown.
3 - You go by each of the rows of the maze.
4 - You convert the row (splitString[x]) to a character array. You take the 1st dimension of the 2D array (although your code initialized this dimension to have length 1). If you still haven't found a problem, you should know that Java arrays are 0-based indexed. Read up more on it. Ignoring this mistake, you further try to store a whole character array on the 2nd dimension, whereas you have not initialized this dimension yet.
So what you need to do:
You need to set the length of the first dimension to the number of rows you will have in the maze.
For each row (of string), initialize the 2nd array dimension to the length of that string, and then set the character array[row] = splitString[x].toCharArray();
You can find a modified version below:
private String stringToMaze(String sMaze) {
String[] splitString = sMaze.split("\n");
char[][] array = new char[splitString.length][];
for (int x = 0; x < splitString.length; x++) {
array[x] = new char [splitString[x].length()];
array[x] = splitString[x].toCharArray();
for (int i = 0; i< splitString[x].length(); i++) {=
System.out.print(array[x][i]);
}
System.out.println();
}
return null; // TO DO
}
Create infinite array of arrays in java
Is there a way to create such array in java?
Yes and no. Java is a strongly typed language. With that said, every object in Java inherits from the built-in class Object
. So you could do something like
Object[] array = new Object[]{1, new Object[]{2, new Object[]{3, /* ... */}}};
Although, using such a construct would be a real pain. You'll need to type-cast values before you can use them as their sourced type again.
Integer a = (Integer) array[0];
Object[] b = (Object[]) array[1];
You can achieve dynamic sizing using List<Object>
.
Related Topics
Dx Bad Class File Magic (Cafebabe) or Version (0033.0000) with Adk14
Android: Prompt User to Save Changes When Back Button Is Pressed
How to Implement a Fileobserver from an Android Service
Android Microsoft Office Library (.Doc, .Docx, .Xls, .Ppt, etc.)
Android Telegram App --> Java.Lang.Unsatisfiedlinkerror: No Implementation Found for Void
Can't Get Location and Email Using Facebook API
How to Serialize Object to JSON
Broadcast Receiver as Inner Class in Android
Android:Change App Label Programmatically
Playing Bg Music Across Activities in Android
Java - Ignore Expired Ssl Certificate
What Is the Certificate Enrollment Process
Jcenter Deprecation; Impact on Gradle and Android
Dynamically Get Drawables by Id
How to Connect Android with MySQL Using MySQL Jdbc Driver
How to Properly Use Backwards Compatible Vector Drawable with the Latest Android Support Library