Help me to add to the list
my code:
List<Map> = [{'a':1}];
I tried List.add(); but it didn't work for me
First give a name to list
you can use the below code:
final List<Map> mylist = [{'a':1}];
mylist.add({'b':2});
print(mylist);
try this
Related
I have a list of n integers. I would like to randomly select three values out of this list and affect them to a three item list.
How can I do that ?
Create this method:
import 'package:collection/collection.dart';
List<int> getList(int n, List<int> source) => source.sample(n);
Usage:
final outputList = getList(3, your_int_list);
print(outputList); // Prints non-repeating 3 random number
Try this exemple with List of String :
import "dart:math";
List<String> list = ['tata','toto','titi','tutu','lala','lolo','lili'];
final random= new Random();
String item= list[random.nextInt(list.length)];
print(item);
With this code select three items from testList1 and add to another list(testList2), but this code sometimes selects the same items. Here is code:
void getRandomItem(){
T getRandomElement<T>(List<T> testList1) {
final random = new Random();
var i = random.nextInt(testList1.length);
return testList1[i];
}
for(var i=0;i<3;i++){
var randomItem= getRandomElement(testList1);
print(randomItem);
testList2.add(randomItem);
print(testList2);
}
}
button event:
onPressed: () {
getRandomItem();
},
i have two list as follows:
record=[1,2,3,4]
colRecord=[a,b,c,d]
I want this two list in following output:
data= ["1":a, "2":b, "3":c, "4":d]
using dart
You can get a Map with Map.fromIterables
var record = [1,2,3,4];
var colRecord = [a,b,c,d];
var result = Map.fromIterables(record, colRecord);
I'm building a bunch of Uint8List (of different sizes, for now they are stored in a generic List) and I need to combine/concatenate them before sending on a websocket.
What would be the best approach ?
I though of combining them in a new Uint8List, but since I don't need byte access anymore after it is combined, I can maybe use a different List<int> implementation ... ?
Thanks in advance.
Using BytesBuilder seems to be the most efficient way to concatenate Uint8List's in Dart:
var b = BytesBuilder();
var l1 = Uint8List(4);
var l2 = Uint8List(4);
b.add(l1);
b.add(l2);
var ll = b.toBytes();
Uint8List implements List<int>. You can combine them to a new List<int> and then create a new Uint8List with
List<List<int>> myByteLists = ...;
var bytes = Uint8List.fromList(myByteList.expand((x) => x).toList());
I try to implement mergeSort in dart. Here is my code:
List mergeSort(List list)
{
if (list.length <= 1) return list;
List left, right, result;
int middle = list.length ~/ 2;
left = mergeSort(list.getRange(0, middle));
right = mergeSort(list.getRange(middle, list.length) as List);
result = merge(left, right);
return result;
}
And I get an TypeError at this line:
left = mergeSort(list.getRange(0, middle));
It seems that getRange() returns Iterable, but List implements Iterable what is the problem here, shouldn't it work?
Anyway when I try:
left = mergeSort(list.getRange(0, middle) as List);
it still doesn't work and gives me a CastError.
So my question is what's the problem here, and what's the Dart-style solution for it?
Thx in advance.
Iterable.toList() should do the trick.
left = mergeSort(list.getRange(0, middle).toList());
You can't directly cast an Iterable to a List because Iterable is a super type of List.
You can cast an object to its parent class, but you cant cast an object to any of its child class.
Example:
var a = new AnyClass();
var o = new Object(); //Actually this is not correct, but let's imagine
(o as AnyClass).toString() //wont works
(a as Object).toString() //will works
The getRange method returns an Iterable, not a List. Using as List does not change that, it just fails because its operand isn't actually a List.
You can use the List.sublist method to get a slice of a list as an actual List.
The collection package on pub also contains an implementation of merge-sort: package:collection/algorithms.dart.
I tried the following code in LINQPad and got the results given below:
List<string> listFromSplit = new List<string>("a, b".Split(",".ToCharArray())).Dump();
listFromSplit.ForEach(delegate(string s)
{
s.Trim();
});
listFromSplit.Dump();
"a" and " b"
so the letter b didn't get the white-space removed as I was expecting...?
Anyone have any ideas
[NOTE: the .Dump() method is an extension menthod in LINQPad that prints out the contents of any object in a nice intelligently formatted way]
you're just creating a trimmed string, not assigning anything to it.
var s = " asd ";
s.Trim();
won't update s, while..
var s = " asd ";
s = s.Trim();
will..
var listFromSplit = "a, b".Split(',').Select(s=>s.Trim());
would, i suppose, be how i'd go about it.
The String.Trim() method returns a string representing the updated string. It does not update the string object itself, but rather creates a new one.
You could do this:
s = s.Trim();
However you cannot update a collection while enumerating through it so you'd want to either fill a new List while enumerating over the existing one or populate the List manually using the string array returned by String.Split.
Filling a new list:
List<string> temp = new List<string>("a, b".Split(",".ToCharArray()));
List<string> listFromSplit = new List<string>();
temp.ForEach(delegate(string s)
{
listFromSplit.Add(s.Trim());
});
listFromSplit.Dump();
Populating Manually:
string[] temp = "a, b".Split(",".ToCharArray());
List<string> listFromSplit = new List<string>();
foreach (string s in temp)
{
listFromSplit.Add(s.Trim());
};
listFromSplit.Dump();
Further to the answer posted by Adrian Kuhn you could do the following:
var result = listFromSplit.Select(s => s.Trim());
The string instances are immutable. Anything that seems to modify one, creates a new instance instead.
You are not assigning the trimmed result to anything. This is a classic error, I've only just got out of the habit of making this mistake with string.Replace :)
I have no IDE up and running, but this should get the job done (unless I am wrong):
var result = from each in listFromSplit select each.Trim();
Split on both spaces and commas and remove any empty entries. All nice and trimmed. Assumes that your strings don't contain spaces, though.
List<string> listFromSplit =
new List<string>( "a , b ".Split( new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries ));
The linq options others have provided should work well. As another option, here is an extension method using a for loop:
public static void TrimCollection(this IList<string> stringCollection) {
for (int i = 0; i <= stringCollection.Count() - 1; i++)
stringCollection[i] = stringCollection[i].Trim();
}