Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following snippet gives an example that shows how to get an element from a <code>List</code> at a specified index, and also how to use the advanced for-each loop to iterate through all elements:</p> <pre><code> import java.util.*; //... List&lt;String&gt; list = new ArrayList&lt;String&gt;(); list.add("Hello!"); list.add("How are you?"); System.out.println(list.get(0)); // prints "Hello!" for (String s : list) { System.out.println(s); } // prints "Hello!", "How are you?" </code></pre> <p>Note the following:</p> <ul> <li>Generic <code>List&lt;String&gt;</code> and <code>ArrayList&lt;String&gt;</code> types are used instead of raw <code>ArrayList</code> type.</li> <li>Variable names starts with lowercase</li> <li><code>list</code> is declared as <code>List&lt;String&gt;</code>, i.e. the interface type instead of implementation type <code>ArrayList&lt;String&gt;</code>.</li> </ul> <hr> <h2>References</h2> <h3>API:</h3> <ul> <li><a href="http://java.sun.com/docs/books/tutorial/collections/index.html" rel="noreferrer">Java Collections Framework tutorial</a></li> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html" rel="noreferrer"><code>class ArrayList&lt;E&gt; implements List&lt;E&gt;</code></a></li> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/List.html" rel="noreferrer"><code>interface List&lt;E&gt;</code></a> <ul> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/List.html#get%28int%29" rel="noreferrer"><code>E get(int index)</code></a> <ul> <li>Returns the element at the specified position in this list.</li> </ul></li> </ul></li> </ul> <h3>Don't use raw types</h3> <ul> <li><p><a href="http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.8" rel="noreferrer">JLS 4.8 Raw Types</a></p> <blockquote> <p>The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of genericity into the Java programming language is strongly discouraged. <em>It is possible that future versions of the Java programming language will disallow the use of raw types.</em></p> </blockquote></li> <li><p><em>Effective Java 2nd Edition: Item 23: Don't use raw types in new code</em></p> <blockquote> <p><em>If you use raw types, you lose all the safety and expressiveness benefits of generics.</em></p> </blockquote></li> </ul> <h3>Prefer interfaces to implementation classes in type declarations</h3> <ul> <li><em>Effective Java 2nd Edition: Item 52: Refer to objects by their interfaces</em> <blockquote> <p>[...] you should favor the use of interfaces rather than classes to refer to objects. <em>If appropriate interface types exist, then parameters, return values, variables, and fields should all be declared using interface types</em>.</p> </blockquote></li> </ul> <h3><a href="http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html" rel="noreferrer">Naming conventions</a></h3> <blockquote> <p>Variables: Except for variables, all instance, class, and class constants are in mixed case with a <em>lowercase first letter</em>.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload