Reputation: 37
I have been googling for 2 days now and I have not found how to create a jagged list(array) in Dart.
There is this Dart - How to initialize a jagged array? but the given answer makes a normal list(array), what i want is something like this:
Upvotes: 0
Views: 107
Reputation: 1
Here's is ur answer:
public class Main {
public static void main(String[] args) {
// Declaring a jagged array
int[][] jaggedArray = new int[5][];
// Initializing arrays of different lengths
jaggedArray[0] = new int[3]; // First row with 3 elements
jaggedArray[1] = new int[4]; // Second row with 2 elements
jaggedArray[2] = new int[2]; // Third row with 4 elements
jaggedArray[3] = new int[1];
jaggedArray[4] = new int[5];
// Assigning values
jaggedArray[0][0] = 10;
jaggedArray[0][1] = 9;
jaggedArray[0][2] = 8;
jaggedArray[1][0] = 7;
jaggedArray[1][1] = 5;
jaggedArray[1][2] = 6;
jaggedArray[1][3] = 88;
jaggedArray[2][0] = 30;
jaggedArray[2][1] = 15;
jaggedArray[3][0] = 90;
jaggedArray[4][0] = 10;
jaggedArray[4][1] = 20;
jaggedArray[4][2] = 30;
jaggedArray[4][3] = 40;
jaggedArray[4][4] = 50;
// Displaying the values of the jagged array
for (int i = 0; i < jaggedArray.length; i++) {
for (int j = 0; j < jaggedArray[i].length; j++) {
System.out.print(jaggedArray[i][j] + " ");
}
System.out.println();
}
}
}
Upvotes: -1
Reputation: 31259
Not sure I understand the question. But is it something like this where we create a list of lists where each list in the list has a different length?
void main() {
final arr = [
[10, 9, 8],
[7, 5, 6, 88],
[30, 15],
[90],
[10, 20, 30, 40, 50]
];
print(arr); // [[10, 9, 8], [7, 5, 6, 88], [30, 15], [90], [10, 20, 30, 40, 50]]
}
Upvotes: 2