Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Is there a way in Java's for-each loop for(String s : stringArray) { doSomethingWith(s); } to find out how often the loop has already been processed? Aside from using using the old and well-known for(int i=0;i<boundary;i++)-loop, is the construct int i = 0; for(String s : stringArray) { doSomethingWith(s); i++; } the only way to have such a counter available in a for-each loop?
Why can't I do: Enumeration e = ... for (Object o : e) ...
I thought I'd offer this softball to whomever would like to hit it out of the park. What are generics, what are the advantages of generics, why, where, how should I use them? Please, keep it fairly basic. Thanks.
Well, I'm pretty much trying to figure out how to pull information from a webpage, and bring it into my program (in Java). For example, if I know the exact page I want info from, for the sake of simplicity a Best Buy item page, how would I get the appropriate info I need off of that page? Like the title, price, description? What would this process even be called? I have no idea were to even...
I often have a need to take a list of objects and group them into a Map based on a value contained in the object. Eg. take a list of Users and group by Country. My code for this usually looks like: Map<String, List<User>> usersByCountry = new HashMap<String, List<User>>(); for(User user : listOfUsers) { if(usersByCountry.containsKey(user.getCountry())) { //...
Does a java for-each loop guarantee that the elements will be presented in order if invoked on a list? In my tests it does seem to, but I can't seem to find this explicitly mentioned in any documentation List<Integer> myList;// [1,2,3,4] for (Integer i : myList) { System.out.println(i.intValue()); } #output 1,2,3,4
I created a class MyList that has a field private LinkedList<User> list; I would like to be able to iterate the list like this: for(User user : myList) { //do something with user } (when my list is an instance of MyList). How? What should I add to my class?
I think most coders have used code like the following : ArrayList<String> myStringList = getStringList(); for(String str : myStringList) { doSomethingWith(str); } How can I take advantage of the for each loop with my own classes? Is there an interface I should be implementing?
How do I get the current index for the song element in Java? for (Element song: question){ song.currentIndex() ?? } In PHP you could do foreach ($arr as $index => $value) { echo "Key: $index; Value: $value"; }
if I do this in Java: for(String s : myCollection.expensiveListGeneration()) { doSomething(); } is expensiveListGeneration() invoked just once at the beggining or in every cycle iteration? Is it implementation dependent?
This might be a bit silly, but where can I find a reference (not code) that tells me, what classes (in the .NET-Framework) implement an interface? Is this even available? I am thinking of the list one gets in the Java-API-documentation unter "All Known Implementing Classes" (like here for example: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Iterable.html). Currently I am wondering what ...
I have this for-each-loop: for (Component c : container.getComponents()) { // Loop code } Is getComponents called on every iteration? Does it make sense to call getComponents before the look and only work on a cached array?
def array = [1,2,3,4,5] println 3 in array prints true. What do I need to overload to support in for any object? Example: class Whatever { def addItem(item) { // add the item } } def w = new Whatever() w.addItem("one") w.addItem("two") println "two" in w I know I could make the collection this class uses public, but I'd like to use in instead.
I am a beginner and I cannot understand the real effect of the Iterable interface.
I can't keep wondering why I can't write something like that: for (int i : 3) { System.out.println(i); } to print out: 0 1 2 I mean, 3 could be auto-boxed into an Integer, which could be Iterable. I know, I've selected the first element to be 0, but I assume it's the common case and that it could facilitate counting down with such ForEach construct.
Sorry for the newbie question, I'm used to C# so my Java framework knowledge is not so good. I have a couple of arrays: int[] numbers = new int[10]; String[] names = new String[10]; //populate the arrays Now I want to make a generic function which will print out the values in these arrays, something like the following (this should work in C#) private void PrintAll(IEnumerable items) { ...
OK, so it's easy to name an interface (or class for that matter) if you can easily think of a noun: User, Window, Database, Stream, etc. What about an adjective or adjective concept? e.g. something that has a timestamp (HasTimestamp, Timestamped, Timestampable...?) or something that is tracked or watched (Trackable, IsTracked, Watchable, IsWatched...?)
The Java Collections.max takes only a collection of a sortable object. However since the collection is not necessarily sorted, I don't see any reason not to implement the same max function for iterable types. Is there a max method for Iterable<T extends Comparable<? super T>> in java's standard library?
If javac do what I think, the following lines would yield the same performance: for (Object o: getObjects()) {} List<Object> os = getObjects(); for (Object o: os) {} Is it so or not? Or is it perhaps implementation-specific? If so: anybody know about GWT?
Suppose I have a base class B, and a derived class D. I wish to have a method foo() within my base class that returns a new object of whatever type the instance is. So, for example, if I call B.foo() it returns an object of type B, while if I call D.foo() it returns an object of type D; meanwhile, the implementation resides solely in the base class B. Is this possible?
I get this error when trying to compile: The method filter(Iterable<T>, Predicate<? super T>) in the type Iterables is not applicable for the arguments (Iterator<PeopleSoftBalance>, ColumnLikePredicate<PeopleSoftBalance>) Here is the ColumnLikePredicate class sig: public class ColumnLikePredicate<T extends RowModel> implements Predicate<T> What am I ...
 /*
  * Copyright 2003-2006 Sun Microsystems, Inc.  All Rights Reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Sun designates this
  * particular file as subject to the "Classpath" exception as provided
  * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */
package java.lang;
Implementing this interface allows an object to be the target of the "foreach" statement.

Since:
1.5
public interface Iterable<T> {

    
Returns an iterator over a set of elements of type T.

Returns:
an Iterator.
    Iterator<T> iterator();
New to GrepCode? Check out our FAQ X