Note Variable objects
The collections class in Java.util is designed to improve Java performance by substituting arrays. As you can see in part 1th, they are also changeable, customizable and scalable in a variety of ways to help achieve quality, concise code.
Collections are very powerful, but many change: use them carefully, and misuse them poses risks.
1. List is different from array
Java developers often mistakenly assume that ArrayList is a substitute for Java arrays. Collections is supported by arrays and performs better when searching for content randomly within a collection. As with arrays, a collection uses an integer ordinal to get a specific item. But the collection is not a simple substitution of arrays.
To understand the difference between an array and a set, you need to figure out the difference between order and position. For example, the list is an interface that holds the order in which items are put into the collection, as shown in Listing 1:
Listing 1. Variable key value
import java.util.*;
public class Orderandposition
{
public static <T> void Dumparray (t[] array)
{
System . OUT.PRINTLN ("=============");
for (int i=0; i<array.length; i++)
System.out.println ("Position" + i + ":" + array[i]);
public static <T> void Dumplist (list<t> List)
{
System.out.println ("============="); br> for (int i=0; i<list.size (); i++)
System.out.println ("Ordinal" + i + ":" + list.get (i));
public static void Main (string[] args)
{
List<string> argList = new Arraylist<strin G> (Arrays.aslist (args));
Dumparray (args);
ARGS[1] = null;
Dumparray (args);
Dumplist (argList);
Arglist.remove (1);
Dumplist (argList);
}
}
When the third element is removed from the list above, its "back" items are raised to fill the vacancy. Obviously, this collection behavior differs from the behavior of the array (in fact, removing an item from an array and removing it from the list is not entirely the same thing-removing an item from an array means overwriting its index slot with a new reference or null).