This question already has answers here:
Difference between assigning the values in parameter list and initializer list
(2 answers)
Closed 6 months ago.
So there is 2 classes where I would like to pass class Two as param to One's constructor.
But dart complains that I did not initiate pro1 and prop2
class Two {
int propA = 1;
int propB = 2;
int propC = 3;
}
class One {
One(Two two){
prop1 = two.propA + two.propB;
prop2 = two.propC + 3;
}
int prop1;
int prop2;
}
Like this:
class Two {
int propA = 1;
int propB = 2;
int propC = 3;
}
class One {
One(Two two)
: prop1 = two.propA + two.propB,
prop2 = two.propC + 3;
int prop1;
int prop2;
}
Class instance variables needs to be defined as part of initializing the object which happens before running the constructor body. To add logic to the initialization, we do that by inserting code after a : (and before an eventually {).
Related
I have an array of int I'd like to create a function which retrieves from this array the closest value to randomInt
final myArray = [10 , 20, 14, 15, 18, 24];
final int randomInt = 21;
getClosestValueInArray(myArray, randomInt); // should return 20
int getClosestValueInArray(List<int> array, int value) {
}
You could loop through the array and check closeness for each element. E.g. in pseudo-code:
closest = myArray[0]
best_closeness = abs(myArray[0] - randomInt)
for (element in myArray) {
closeness = abs(element - randomInt)
if (closeness < best_closeness) {
closest = element
best_closeness = closeness
}
}
return closest
And in Dart (added by #julemand101)
int getClosestValueInArray(List<int> array, int value) {
int closest = array.first;
int best_closeness = (closest - value).abs();
for (int element in array.skip(1)) {
int closeness = (element - value).abs();
if (closeness < best_closeness) {
closest = element;
best_closeness = closeness;
}
}
return closest;
}
One option is to put these elements in a TreeSet and then use ts.floor(randomInt) and ts.ceil(randomInt) to check what the nearest elements to randomInt is, and pick the one with the smallest absolute difference to randomInt.
Another option is to put these values in a hashset and then search for randomInt - 1, randomInt + 1, randomInt - 2 and so on. This again has benefits and drawbacks compared to the previous approach.
There are many other ways.
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 have a string hiWorld and
i want to split this string in two parts hi and World by length of first word hi which is of length 2.
This is what i want to do
List<String> list = ("hiWorld").splitFromLength(2);
I'd use the solution you published shortening up the definition:
List<String> splitStringByLength(String str, int length) =>
[str.substring(0, length), str.substring(length)];
or using an extension method to call the function:
extension on String {
List<String> splitByLength(int length) =>
[substring(0, length), substring(length)];
}
'helloWorld'.splitByLength(5); // Returns [hello, World].
My current solution
List<String> splitStringByLength( String str, int length)
{
List<String> data = [];
data.add( str.substring(0, length) );
data.add( str.substring( length) );
return data;
}
This is my solution which is more generic:
List<String> splitByLength(String value, int length) {
List<String> pieces = [];
for (int i = 0; i < value.length; i += length) {
int offset = i + length;
pieces.add(value.substring(i, offset >= value.length ? value.length : offset));
}
return pieces;
}
And the extension method:
extension on String {
List<String> splitByLength(int length, {bool ignoreEmpty = false}) {
List<String> pieces = [];
for (int i = 0; i < this.length; i += length) {
int offset = i + length;
String piece = this.substring(i, offset >= this.length ? this.length : offset);
if (ignoreEmpty) {
piece = piece.replaceAll(RegExp(r'\s+'), '');
}
pieces.add(piece);
}
return pieces;
}
}
You can use it like:
'HELLO WORLD'.splitByLength(5, ignoreEmpty: true)
I'm trying to convert an integer variable
var int counter = 0;
into a string variable
var String $counter = "0";
I searched but I only found something like
var myInt = int.parse('12345');
that doesn't work with
var myInt = int.parse(counter);
Use toString and/or toRadixString
int intValue = 1;
String stringValue = intValue.toString();
String hexValue = intValue.toRadixString(16);
or, as in the commment
String anotherValue = 'the value is $intValue';
// String to int
String s = "45";
int i = int.parse(s);
// int to String
int j = 45;
String t = "$j";
// If the latter one looks weird, look into string interpolation on https://dart.dev
You can use the .toString() function in the int class.
int age = 23;
String tempAge = age.toString();
then you can simply covert integers to the Strings.
say i have the following two struct definitions in C.
struct child {
int x;
};
struct Yoyo {
struct child **Kids;
};
How would i go about allocating the memory for Kids.
say for example i have some function Yoyo_create().
static struct Yoyo * yoyo_create() {
int n = 32000;
struct Yoyo *y;
y = malloc(sizeof( *Yoyo));
y->Kids = malloc(n*sizeof(*Child));
for (i = 0 ; i < n ; i ++) { y->Kids[i] = NULL; }
}
and then to destroy the Kids in some "destructor function" i would do.
void yoyo_destroy(struct yoyo *y)
{
free(y->Kids);
free(y);
}
Does that make sense?
you don't need these lines
y->Kids = malloc(n*sizeof(*Child)); and <br>
free(y->Kids);
because your y contains kids structure in it. And except these , you are going well
y = malloc(sizeof(struct Yoyo));
y->Kids = malloc(n*sizeof(struct Child));