Why filled Dart list adds to all its elements? - arrays

I have a simple code where I create a filled list of integer lists and add an integer to the first element of the list.
List<List<int>> list = List<List<int>>.filled(2, []);
list[0].add(1);
print(list);
What I expect is: [[1],[]]
But in console I get: [[1], [1]]
I can't understand this behavior, can someone explain why this is happening?

You likely meant to use List.generate. The Dart documentation here references this exact situation. By using filled you are explicitly stating that all of the elements have the same fill value.

Related

No exact matches in call to subscript, reversed array in Swift

I am trying to understand different Swift's basics better. I bumped on the reversed array concept in Paul Hudson's videos.
He said that the array will be printed in the same order as the original one, but also that other operations will be done on the reversed version of the array.
And so I did this:
let presidents = ["Bush", "Obama", "Trump", "Biden"]
let reversedPresidents = presidents.reversed()
print(reversedPresidents[2])
And I received:
No exact matches in call to subscript
error.
Why?
It's explained reversed documentation.
First of all, it returns the type ReversedCollection<Array<Element>>
Secondly, it doesn't really change the order of array, as explained here:
or ordinary collections c having bidirectional indices:
c.reversed() does not create new storage
c.reversed().map(f) maps eagerly and returns a new array
In other words: reversed() is able to iterate the provided array in reverse order, but does not create a reversed array
So if you want to create a reversed array, you can either do this:
let reversedPresidents = presidents.reversed().map { $0 }
or, as mentioned in comment above,
let reversedPresidents = Array(presidents.reversed())

How does Ruby's Combined Comparison Operator work?

First question on stackoverflow :)
I'm going through the Ruby course on Codecademy and I'm stuck on something.
fruits = ["orange", "apple", "banana", "pear", "grapes"]
fruits.sort! {|first, second| second <=> first}
print fruits
I don't know how to phrase this question. On Codecademy the assignment was to set up the array to be displayed in reverse on the console. After some research, I was able to figure it out. I understand how it works and the order to put it in the code not why. I'm aware that "<=>" compares two objects, but how do the items within the array become objects when we don't declare them as such?
Secondly, what is the purpose of writing this code in this way when we could do fruits.sort.reverse?
First question: At various points in its operation the sort method has to compare pairs of objects to see what their relative ordering should be. It does the comparison by applying the block you pass to sort, i.e., {|first, second| second <=> first}. Not sure what you mean by "how do the items within the array become objects when we don't declare them as such?". All data in ruby is an object, so there's no declaration or conversion needed given that all variables are object references.
Second question: Yes, you could do fruits.sort.reverse, but that would require additional work after the sort to do the reverse operation. Also, reverse can't handle more complex sorting tasks, such as sorting people by multiple criteria such as gender & last name, or hair color, height, and weight. Writing your own comparator can handle quite complex orderings.
String literals can be used to create string objects in Ruby, there is no need to use the String class to create the object. The following two are equivalent:
"Hello, world!"
String.new("Hello, world!")
More information can be found here.
Secondly, what is the purpose of writing this code in this way when we could do fruits.sort.reverse?
Please contact Codecademy about this, but I suspect it's for learning more about how <=> works.

Array (class) filled with non nil values stays empty

I am currently having trouble filling up an array of customClass.
I try to fill it with a jsonFile. During my json parsing (using swiftyJSON) i loop and fill my array.
The problem is, at the end of my loop, it is still empty. I tested it in different ways, and here is my code:
That's the file where the problem is. In my loop I fill an Annotation, that I add with append to my array. The problem is what my print return. Here is a part of it:
It's just a small part of a huge jsonfile. And, my tmpAnnot.name is correctly printed every iteration. But when it comes to my Array, nothing.
So I'm completly lost and hope you could help me ^^
(And for the information, here is my custom class) :
And btw, I tried to print my array.count, and it's nil too
Im so sorry if the question has been posted. I couldn't find it in the entire website.
Change your JSONAnnotationList declaration to be an non-optional and assign it an empty array
var JSONAnnotationList: [UGOAnnotation] = []
You see, you have never created an array so there was nothing to be printed.
The whole point of optionals is to use them sparingly, not everywhere.

Modifying array items in Swift [duplicate]

This question already has answers here:
Swift Object reference to array?
(4 answers)
Closed 6 years ago.
I found some really strange behavior in Swift. Here's the code:
var array2d: [[Int]] = [[1]]
print(array2d) // prints [[1]]
var first = array2d[0]
first.append(2)
print(array2d) // still prints [[1]]!!!
I would totally expect the last line to print [[1, 2]]. I can't explain the current behavior. I'd expect array2d[0] to return a reference to the first item, or possibly a copy of that reference. In either case, modifying that object should modify array2d. But that's not what's happening.
If, however, I update the array like this:
array2d[0].append(2)
it then prints [[1, 2]], as expected.
Can someone please explain this for me?
How arrays are referenced/passed around/copied in swift is a point of a lot of contention, take a look at this link.
In essence what is happening is that var first = array2d[0] is taking a copy of the array at that index as opposed to creating a reference as you were expecting. Hence accessing the array with the subscript notation allows to you to correctly alter the array but creating a new variable doesn't.

Array won't refresh itself after removeChild

There is an array of objects. I'm trying to removeChild an object from that array like below. removeChild works fine but the array won't refresh itself after removing uppest object. As you can see in below, i tried to trace array items out.
Firstly, array has three items, obviously the myArray.length must be 3.
After removing a child, myArray.length must be 2, but it get 3 (Wrong).
removeChild(myArray[currShape]);
trace(myArray);
Please tell me what am i missing here.
Assuming you're using ActionScript, removeChild() only serves to take objects off the stage. It doesn't take things out of an array. You have to take the object out of the array manually in another statement.
You could try something like:
removeChild(myArray.splice(currShape,1));
This removes the entry from the array and returns that entry that will be used to remove it from the stage.

Resources