How would you explain to someone who has just started programming in Java, what the difference between ArrayLists and Iterators are?
Why I would use an iterator instead of using the get() methods of the Arraylist
...
Consider this bit of obfuscated code. The intention is to create a new object on the fly via the anonymous constructor and yield return it. The goal is to avoid having to maintain a local collection just to simply return it.
public static List<DesktopComputer> BuildComputerAssets()
{
List<string> idTags = GetComputerIdTag...
var trimmed = myStringArray.Select(s => s.Substring(0, 10));
If one of the strings isn't 10 characters long I'd get an ArgumentOutOfRangeException.
In this case its fairly trivial to find out and I know I can do
s.Substring(0, Math.Min(10, s.Length))
With more complex object construction errors like this aren't always easy to se...
What is the pattern (best practice) for such problem -- modifying elements (values) in collection?
Conditions:
size of the collection is not changed (no element is deleted or added)
modification is in-place
In C++ it was easy and nice, I just iterated trough a collection and changed the elements. But in C# iterating (using enumerato...
Given a base class with the following interface:
public class Base
{
public virtual IEnumerable<string> GetListOfStuff()
{
yield return "First";
yield return "Second";
yield return "Third";
}
}
I want to make a derived class that overrides the method, and adds its own stuff, like so:
public class D...
Hello,
I have a class that includes a std::list and wish to provide public begin() and end() for const_iterator and private begin() and end() for just plain iterator.
However, the compiler is seeing the private version and complaining that it is private instead of using the public const version.
I understand that C++ will not overload...
PLEASE CHECK ANSWERS by VolkerK too, he provided another solution, but I can't mark two posts as answer. :(
Good day!
I know that C# allows multiple iterators using yield, like described here:
http://stackoverflow.com/questions/1754041/is-multiple-iterators-is-possible-in-c
In PHP there is and Iterator interface. Is it possible to i...
I have a generator (numbers) and a value (number). I would like to iterate over these as if they were one sequence:
i for i in tuple(my_generator) + (my_value,)
The problem is, as far as I undestand, this creates 3 tuples only to immediately discard them and also copies items in "my_generator" once.
Better approch would be:
def con(...
Ok so I have an ArrayList (arrBok), which is full of book objects (the code is in Norwegian, so pay no attention to that please). I want to make a public method which iterates through all the objects in the arraylist.
When I execute the code, it just seems to run in an infinite loop, not producing any return values.
Here is the relevan...
$arrayIter = new ArrayIterator( array(1, 2) );
$iterIter = new IteratorIterator($arrayIter);
var_dump($iterIter->valid()); //false
var_dump($arrayIter->valid()); //true
If I first call $iterIter->rewind(), then $iterIter->valid() is true. I'm curious why it requires that rewind() be called. I imagine there's good reason for it, but I ...
I'd like to use ftplib to upload program-generated data as lists. The nearest method I can see for doing this is ftp.storlines, but this requires a file object with a readlines() method.
Obviously I could create a file, but this seems like overkill as the data isn't persistent.
Is there anything that could do this?:
session = ftp.new(...
I'm busy implementing a Graph ADT in C++. I have templates for the Edges and the Vertices. At each Vertex I have a vector containing pointers to the Edges that are incident to it. Now I'm trying to get an iterator over those edges. These are the lines of code:
vector<Edge<edgeDecor, vertexDecor, dir>*> edges = this->incidentEdges();
vec...
Suppose I have a function which can either take an iterable/iterator or a non-iterable as an argument. Iterability is checked with try: iter(arg).
Depending whether the input is an iterable or not, the outcome of the method will be different. Not when I want to pass a non-iterable as iterable input, it is easy to do: I’ll just wrap it w...
Hi all,
I have the following code (just typed it in here, might have typos or stuff):
typedef boost::ptr_vector<SomeClass> tvec;
tvec v;
// ... fill v ...
tvec vsnap;
for(tvec::iterator it = v.begin(); it != v.end(); ++it)
{
if((*v).anyCondition)
vsnap.push_back( it ); // (*it) or &(*it) doesn't work
}
My problem is now ...
Hi all,
I am a C++ newbie trying to use a map so I can get constant time lookups for the find() method.
The problem is that when I use an iterator to go over the elements in the map, elements do not appear in the same order that they were placed in the map.
Without maintaining another data structure, is there a way to achieve in order...
I have a bunch of containers of object pointers that I want to iterate through in different contexts to produce diagnostics for them. I'm struggling with the syntax required to define the functions... which, on account of these objects filtering through diverse parts of my application, seem best encapsulated in a dedicated diagnostics cl...
Hi,
I'm using the following code to get an array of directories and their sub-directories where each contain file type extension: png. It works great but I need to be able to output the results of the array in a list style format e.g.
* Test
-> test2.png
-> test1.png
* subfolder
-> test3.png
* sub sub folder
-> test...
I can't seem to find any information on this, so I turn to stackoverflow. How efficient are the iterators of std::tr1::unordered_map in C++? Especially compared to, say, list iterators. Would it make sense to make a wrapper class that also holds all the keys in a list to allow for efficient iteration (my code does use a lot of iterati...
If all that you're doing is a simple one-pass iteration (i.e. only hasNext() and next(), no remove()), are you guaranteed linear time performance and/or amortized constant cost per operation?
Is this specified in the Iterator contract anywhere?
Are there data structures/Java Collection which cannot be iterated in linear time?
java.uti...
I try to do it in the following way:
public String getValue(String service, String parameter) {
String inputKey = service + ":" + parameter;
Set keys = name2value.keySet();
Iterator itr = keys.iterator();
while (itr.hasNext()) {
if (inputKey.equal(itr.next())) {
return name2value.get(inputKey);
...