Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
swdev:java:lang:arraylist [2017/10/25 08:32]
smayr
swdev:java:lang:arraylist [2017/10/25 08:49] (current)
smayr [ArrayList]
Line 1: Line 1:
-ArrayList =+List = 
 +List is a generic class that can handle most data.  
  
 +Initializing a ''List'':
 +<code java>
 +import java.util.ArrayList;
 +import java.util.List;
 +
 +// Method 1
 +List<Object> arr = Arrays.asList(
 +    1, "10001", "Acme Inc.", 1, null 
 +);
 +
 +// Method 2
 +List<Object> arr = new ArrayList<Object>();
 +arr.add(1);
 +arr.add("10001");
 +arr.add("Acme Inc.");
 +arr.add(1);
 +arr.add(null);
 +</code>
 +
 += ArrayList =
 +''ArrayList'' is derived from ''List''. It can handle mixed primitives (as ''Object'').
 <code java> <code java>
 import java.time.LocalDateTime; import java.time.LocalDateTime;
Line 31: Line 53:
 </code> </code>
  
-= List = +To process each object with the correct type:
-List is a generic class that can handle most data.   +
- +
-Initializing a ''List'':+
 <code java> <code java>
-import java.util.ArrayList+ArrayList<Object> listOfObjects = new ArrayList<Object>(); 
-import java.util.List; +for(Object obj: listOfObjects{ 
- +   if (obj instanceof String{ 
-// Method 1 +       // handle String 
-List<Object> arr = Arrays.asList( +   } else if (obj instanceof Integer{ 
-    1, "10001", "Acme Inc.", 1, null  +       // handle Integer 
-); +   } else { 
- +       // handle others 
-// Method 2 +   } 
-List<Object> arr = new ArrayList<Object>(); +}
-arr.add(1); +
-arr.add("10001"); +
-arr.add("Acme Inc."); +
-arr.add(1); +
-arr.add(null);+
 </code> </code>