Iterator worked properly for vector . How? Why? - collections

whenever we read difference between any arraylist and vector. It's said we use iterator for arraylist and enumerator for vector. i used iterator for vector. it worked properly.How? Is it newly added feature ?

Related

How can I return the last array inside this recursive method?

I'm coding a calculator that simplifies a string of operations to arraylists of positive and negative results.
Right now, im trying to deal with adding the parenthesees feature and I thought about creating arraylists inside others to represent the operations within them.
There is only 1 method that processes everything, so when I add "(" an arraylist must be created in the current position of the array, and signify in some way to the rest of the code that next time the method goes in, it must start in that inner array.
So i thought about doing a recursive method that returns the "deepest" arraylist that has no more arrays inside.
public ArrayList<Object> deepest(ArrayList<Object> al){
ArrayList<Object> input=new ArrayList<>(al);
for(Object o:input){
if(o instanceof ArrayList){
return deepest((ArrayList<Object>) o);
}
}
return input;
}
Is this actually returning the deepest array inside the "tree" ? My code is doing some unexpected stuff and I feel like this starts to escape my understanding.
Your approach would return a leftmost ArrayList, not the deepest (the very first it finds in the tree). For the deepest, you would need a more exhaustive search, a counter that keeps track of the current depth, and also calculation of the "deepest so far" item at the appropriate places (after visiting all elements of an ArrayList).

What is the correct way to handle pointer structs in loops in Go?

In this example on the go playground, you can see that looping over a list of objects and putting them into an array of pointer structs ends up putting the same entry into the array multiple times.
http://play.golang.org/p/rICA21kFWL
One possible solution to the issue is to make a new string and sprint the string out of the looped string into the new string. This seems silly though.
What is the idiomatically correct way to handle this problem?
In case I understood correctly and you simply want an array of pointers pointing to the respective string in the original array, you can always do this
# choose correct size from beginning to avoid costly resize
o := make([]*string, len(f))
# iterate only over index
for i := range f {
o[i] = &f[i].username
}
Here's your go playground with the changes sketched out above.

Error with QMutableListIterator

After appending an item to a QList which is pointed to by a QMutableListIterator, I find out the next value of the iterator points outside the list.
Or can't I point the iterator to any where in the list except the beginning or end?
Please I need help.
From the Qt documentation:
no changes should be done directly to the list while the iterator is active (as opposed to through the iterator), since this could invalidate the iterator and lead to undefined behavior.
Appending to the list might cause it's memory to be reallocated, which would mean that the iterator points to an invalid location.

QT elements in vector initialization. How to solve private copy constructor problem?

I wanted to create a vector of a subclass of QGraphicsRectItem, named MyRect. This vector is initialized in MyClass:
MyClass::MyClass () : myVector_(80, std::vector<MyRect>(60, MyRect(true,true)))
...
I learned that vector constructs the first element and then copies it with the copy constructor. The problem is that QGraphicsRectItem's copy constructor is private and this doesn't work. (Very long error message, one hour of googling)
Now I have three possible solutions as I see it:
1.)Make a for-loop and populate myVector myself in the constructor body.
1b.) Just use regular array because it remains static anyway.
2.)Use MyRect* instead of MyRect as content of myVector (manual memory allocation -> bad)
3.)Use QVector that uses Object* by default and manages the memory for me.
After spending at least one hour on solving this I would like to hear from you if there are other good possibilities or what you think is the best solution. I am on the verge of dropping vectors for this and just using arrays.
The vector, as you declared it, will have to manipulate instances of MyRect. This means that depending of what you do the with the elements of the vector, or if you copy the vector, the MyRect instances might be duplicated.
This is not possible, because that would mean creating a new item each time a copy occurs (this is why the QGraphicsItem constructor is private). You have to manipulate the items of your scene through a pointer.
Thus, to me the best solution is to store in your vector pointers on your items (your 2nd solution) :
std::vector<MyRect*>
Memory management shouldn't be a problem at all, as this will be handled by Qt : when you destroy the scene, all items part of this scene will be destroyed.
Your vector won't duplicate items (no instanciation), only pointers, which means you won't create new items you'd have to destroy yourself.

Weird problem with a custom bindable class and a vector

I'm having a very weird problem with a vector in my application.
Details...
I have the following classes.
Person,Player,PlayerController.
Player extends Person. Person extends ObjectProxy in order to enable binding.
So the Player class has the [Bindable] tag.
The PlayerController class contains a remote object calling a php method to receive a firstname and a lastname and when the CallResponder gets the result from the call,the result handler creates a Player instance. At that moment I am trying to push the player object into a Vector..
The problem is the following.
Every time the push method is called, the vector is being populated with the last player that was created but not just in the end of the vector. It replaces the other instances as well! So the vector always contains the most recent player instance but in every position of it. :S
I have also tried doing it with an Array and the results are the same.
Any thoughts on what I'm doing wrong? It's driving me crazy. :S
My guess is that you are pushing the same object reference into your vector after setting that reference to a new instance of Player, meaning that all of the items in your vector refer to the same object, which is always the newest object. I say "guess" because I haven't seen your code. What are you pushing into your vector, a local variable? A member variable?
Edit: Based on your comment below, try adding your new Player object to your vector using a local variable rather than from your member variable (player_):
var newPlayer:Player = new Player();
newPlayer.firstName = results[firstName];
newPlayer.lastName = results[lastName];
players_.push(newPlayer);
player_ = newPlayer;
You are doing what I suspected, which is adding multiple references to the same object to your vector. Since all of the references in your object refer to the same object, changing the one object changes ALL of the entries in your vector. Doing the above will create a brand new (and unique) Player object each time you add to your vector.

Resources