Creating 2D array in Dart where only row length is defined - flutter

I want to implement below piece of code(Java) in Dart/Flutter.
Could you please tell me how to achieve this.?
Here rows = 8, colums = not defined.
int[][] myArray = new int[8][];

Dart represents arrays in the form of List objects. A List is simply an ordered group of objects. ... Each element in the List is identified by a unique number called the index.
To get exactly what you want, you could use double List.generate, i.e.:
const cols = 31;
const rows = 12;
final array = List.generate(rows,
(i) => List.generate(cols + 1, (j) => i + j * cols + 1,
growable: false),growable: false);
array.forEach((row) {
print(row);
});
// [1, 32, 63, ...
// [2, 33, 64, ...
// [3, 34, 65, ...
// ...
There is also List.filled, where you can fill the array with some initial value when created. The following init the 2d array with 0s.
final array = List.generate(rows + 1, (i) => List.filled(cols + 1, 0,growable: false), growable: false);

Related

How to map an array into a List of Vector2s in Flutter for FlutterFlame?

I need to convert an array from Firebase into a List of Vector2s in Flutter for FlutterFlame.
From what I gather from the comments you have an array of integers like this:
final numbers = [1, 2, 3, 4, 5, 6, 1, 43];
You can convert this to a list of Vector2 in many ways, this is one way to do it:
final vectors = [
for(int i = 0; i < numbers.length-1; i+=2)
Vector2(numbers[i].toDouble(), numbers[i+1].toDouble())
];

Splitting integer array into multiple chunks in different length in Dart

List<int> mainList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
List<int> chukLengths= [2,4,4,4,2]
Expected Result:
[[1,2],[3,4,5,6],[7,8,9,10],[11,12,13,14],[15,16]]
I wanna split mainList to 5 chunks with mentioned chunkLength.
How to resolve it in Dart?
You can iterate over the array of your chunks and slice off necessary chunks off the main array. Each iteration your get the chunk itself by using sublist() and then you delete the chunk from the initial array using removeRange()
Here is the code:
List<int> mainList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
List<int> chukLengths= [2,4,4,4,2];
List<List<int>> splitIntoChunks(List<int> list, List<int> chunks) {
List<List<int>> result = [];
for (int chunk in chunks) {
result.add(list.sublist(0, chunk));
list.removeRange(0, chunk);
}
return result;
}
void main() {
print(splitIntoChunks(mainList, chukLengths)); // [[1, 2], [3, 4, 5, 6], [7, 8, 9, 10], [11, 12, 13, 14], [15, 16]]
}

How to find duplicate elements length in array flutter?

I want to implement add to checkout in which number of items added is displayed. Plus button adds elements in list and minus removes elements from list. Goal is just to display particular items added and its quantity. I have added items in list want to count length of duplicate items. How we can do that in flutter?
here is your solution. [Null Safe]
void main() {
List<int> items = [1, 1, 1, 2, 3, 4, 5, 5];
Map<int, int> count = {};
items.forEach((i) => count[i] = (count[i] ?? 0) + 1);
print(count.toString()); // {1: 3, 2: 1, 3: 1, 4: 1, 5: 2}
}

The ten's digit and unit's digit of numbers

I have this code
int[,] array = new int[,]{ {34, 21, 32, 41, 25},
{14 ,42, 43, 14, 31},
{54, 45, 52, 42, 23},
{33, 15, 51, 31, 35},
{21, 52, 33, 13, 23} };
for (int i = 0; i < array.GetLength(1); i++)
{
for (int j = 0; j < array.GetLength(0); j++)
{
Console.Write(array[i, j] + " ");
}
Console.WriteLine();
}
and i need to find a specific number ( the treasure ).
For each value the ten's digit represents the row number and the unit's digit represents the column number of the cell containing the next clue.
Starting in the upper left corner (at 1,1), i have to use the clues to guide me search of the array. (The first three clues are 11, 34, 42).
The treasure is a cell whose value is the same as its coordinates.
The program should output the cells it visits during its search.
I did the simply way:
Console.WriteLine("The next clue is: {0}", array[0, 0]);
Console.WriteLine("The next clue is: {0}", array[2, 3]);
Console.WriteLine("The next clue is: {0}", array[3, 2]);
Console.WriteLine("The next clue is: {0}", array[0, 4]);
and so on, but the problem is, that if I change the array to set another route the program will output the wrong way. So the solution needs to be dynamic and find the treasure regardless of the array content.
My problem is that i don't know how to do to find the ten's digit of the numbers and the unit's digit.
Can anyone please help me with this?
To illustrate my comment: code below and Fiddle
(I've added a HashSet<int> to track which cells have already been visited and avoid ending up with an infinite loop)
int[,] array = new int[,]
{
{34, 21, 32, 41, 25},
{14 ,42, 43, 14, 31},
{54, 45, 52, 42, 23},
{33, 15, 51, 31, 35},
{21, 52, 33, 13, 23}
};
int currentCoordinates = 11;
bool treasureFound = false;
var visitedCells = new HashSet<int>();
while (!treasureFound && !visitedCells.Contains(currentCoordinates))
{
int currentRowIndex = currentCoordinates / 10;
int currentColumnIndex = currentCoordinates % 10;
int nextCoordinates = array[currentRowIndex - 1, currentColumnIndex - 1];
if (nextCoordinates == currentCoordinates)
{
treasureFound = true;
}
else
{
visitedCells.Add(currentCoordinates);
currentCoordinates = nextCoordinates;
}
}
if (treasureFound)
{
Console.WriteLine($"Treasure found in cell {currentCoordinates}");
}
else
{
Console.WriteLine("No treasure");
}

Iterating over a list in groups of two or more

I like to iterate over a list and split them up in couples like this:
List<String> list = [1,2,3,4,5,6,7,8];
List<Tuple2> listOfTuples = list.take2((value1,value2) => Tuple2(value1,value2));
print(listOfTuples.toString()); // output => [[1,2],[3,4],[5,6],[7,8]]
I know there is a take(count) in dart but I did not find a good example.
I know I can do it with a for loop etc. but I am wondering if there us a more elegant way.
There is nothing built in. The way I'd write this today is:
var list = [1, 2, 3, 4, 5, 6, 7, 8];
var tuples = [
for (int i = 0; i < list.length - 1; i += 2) Tuple2(list[i], list[i + 1]),
];
You could write an extension that gives an api take2 on List that could be used in the way you describe.
extension Take2<T> on List<T> {
List<R> take2<R>(R Function(T, T) transform) => [
for (int i = 0; i < this.length - 1; i += 2)
transform(this[i], this[i + 1]),
];
}