I want list subtype to another subtype how can i?
List<type1> someList = new List<type1>();
I want convert this list to
List<type2> someList = new List<type2>();
I have both model with same string its possible? i try this
List<type2> tList = new List();
for (int i = 0; i < snapshot.data.length; i++) {
tList.add(snapshot.data[i]);
}
error:
type 'type1' is not a subtype of type 'type2'
final someList = <TypeA>[];
final convertedList = List<TypeB>.from(someList);
A simple example would be:
final numList = <num>[1, 2];
final intList = List<int>.from(numList);
You might run into errors, if your num contains a double, in that case, you can use map:
final numList = <num>[1, 2.0];
final intList = numList.map((e) => e.toInt()).toList();
Related
The current code:
class A {
List<int> listOne = [];
List<int> listTwo = [];
List≤int> listOfLists = [
...listOne,
...listTwo
];
}
Results in the following error for each list with an spread operator (...):
error: The instance member 'listOne' can't be accessed in an initializer.
error: The instance member 'listTwo' can't be accessed in an initializer.
What I know:
listOne etc. can't be referenced in another initializer
So what I tried: https://dart.dev/tools/diagnostic-messages#implicit_this_reference_in_initializer
class C {
int x;
C() : x = defaultX;
static int get defaultX => 0;
}
Unfortunately, I do not know how to translate that to solve my problem.
Can you guys help me out?
You need to explicitly make a constructor and do the assignment there.
class A {
List<int> listOne = [];
List<int> listTwo = [];
List<int> listOfLists;
A() {
listOfLists = [...listOne, ...listTwo];
}
}
If you're using null-safety then you should add the late keyword.
class A {
List<int> listOne = [];
List<int> listTwo = [];
late List<int> listOfLists;
A() {
listOfLists = [...listOne, ...listTwo];
}
}
I'm trying to convert strings to arrays then compare two arrays. If the same value needs to remove from both array. Then finally merge two arrays and find array length. Below is my code
String first_name = "siva";
String second_name = "lovee";
List<String> firstnameArray=new List();
List<String> secondnameArray=new List();
firstnameArray = first_name.split('');
secondnameArray = second_name.split('');
var totalcount=0;
for (int i = 0; i < first_name.length; i++) {
for (int j = 0; j < second_name.length; j++) {
if (firstnameArray[i] == secondnameArray[j]) {
print(firstnameArray[i] + "" + " == " + secondnameArray[j]);
firstnameArray.removeAt(i);
secondnameArray.removeAt(i);
break;
}
}
}
var finalList = new List.from(firstnameArray)..addAll(secondnameArray);
print(finalList);
print(finalList.length);
But always getting this error Unsupported operation: Cannot remove from a fixed-length list can you help me how to fix this issue. Thanks.
Seems like what you are trying to do is to find the length of unique characters in given two strings. Well, the Set type is perfect for this use-case. Here's an example of what you can do:
void main() {
String first = 'abigail';
String second = 'allie';
var unique = '$first$second'.split('').toSet();
print(unique);
}
This would give you an output of:
{a, b, i, g, l, e}
On which you may perform functions like .toList(), or .where() or .length.
You can ensure that firstnameArray, secondnameArray is not a fixed-length list by initializing it as below:
var firstnameArray = new List<String>.from(first_name.split(''));
var secondnameArray= new List<String>.from(second_name.split(''));
Thereby declaring firstnameArray, secondnameArray to be a mutable copy of input.
How do I implement a method with a variable number of arguments?
In C#, we can use the params keyword:
public class MyClass
{
public static void UseParams(params int[] list)
{
for (int i = 0; i < list.Length; i++)
{
Console.Write(list[i] + " ");
}
Console.WriteLine();
}
}
So how can I do this in F#?
type MyClass() =
member this.SomeMethod(params (args:string array)) = ()
I receive the following error from the code above:
The pattern discriminator 'params' is not defined
You can use ParamArrayAttribute:
type MyClass() =
member this.SomeMethod([<ParamArray>] (args:string array)) = Array.iter (printfn "%s") args
then:
let mc = MyClass()
mc.SomeMethod("a", "b", "c")
how to return an array of unowned strings that all point to the same location in memory?
example:
init
var str = "ABC"
var unowned_string_array = repeat (str, 5)
def repeat (s: string, n: int): array of string
// code
and this array will contains 5 elements(same string "ABC"), all point to same location
The closest Vala code I could get is:
int main() {
var str = "ABC";
var unowned_string_array = repeat (str, 5);
return 0;
}
public (unowned string)[] repeat (string s, int n) {
var a = new (unowned string)[n];
for (var i = 0; i < n; i++)
// This sadly still duplicates the string,
// even though a should be an array of unowned strings
a[i] = s;
return a;
}
I'm not sure if the compiler understands the parenthesis here, it may think that I want to declare an unowned array of owned strings here ...
Update: It turns out the problem is that type inference will always create an owned variable (see nemequs comment).
There is even a bug report for this.
So this works just fine (no string duplication in the repeat function):
int main() {
var str = "ABC";
(unowned string)[] unowned_string_array = repeat (str, 5);
return 0;
}
public (unowned string)[] repeat (string s, int n) {
(unowned string)[] a = new (unowned string)[n];
for (var i = 0; i < n; i++)
// This sadly still duplicates the string,
// even though a should be an array of unowned strings
a[i] = s;
return a;
}
Which would be something like this in Genie:
[indent=4]
init
var str = "ABC"
unowned_string_array: array of (unowned string) = repeat (str, 5)
def repeat (s: string, n: int): array of (unowned string)
a: array of (unowned string) = new array of (unowned string)[n]
for var i = 1 to n
a[i] = s
return a
The Genie code has the additional problem of not compiling, due to the parser not being able to deduce what comes after the array of.
This seems to be a similar problem to what I already had with nested generic types.
I have reported this a Genie bug.
An array of Image Url path:
["http://cdncms.fonts.net/hero-images/FEX_Hero_Thumb.png ","http://cdncms.fonts.net/hero-images/Wilma_Hero_Thumb.png "]
asp.net mvc Controller :
public ActionResult Extract(string[] name)
{
//List<string> myList = name.ToList<string>();
for(int x = 0; x < name.Length; x++)
{
//string something = name[x];
var item = name[x];
Session["values"] = item;
}
char[] delimiter1 = new char[] { ',' }; // <-- Split on these
string[] array1 = Session["values"].ToString().Split(delimiter1, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in array1)
{
string exts = Path.GetExtension(item); //illegal character
string strRealname = Path.GetFileName(item); illegal character
}
I know this problem due to the presence of [ character at the beginning and last .I have tried but not succeeded .Any idea how to remove this using split function in C#
Assuming that you have correctly identified the problem, you could do this before making it an array:
var values = Session["values"].ToString();
if(values.StartsWith("[")) values = values.Substring(1);
if(values.EndsWith("]")) values = values.Substring(0, values.Length - 1);