[code=Java]public class CodeIt
{
public static void main(String[] args)
{
List<Integer> firstAL = new ArrayList<>();
firstAL.add(1);
System.out.println("AL: " + firstAL.toString());
firstAL.add(2);
System.out.println("AL: " + firstAL.toString());
List<Integer> firstLL = new LinkedList<>();
firstLL.add(1);
System.out.println("LL: " + firstLL.toString());
firstLL.add(2);
System.out.println("LL: " + firstLL.toString());
int range = 1_000_000;
List<Integer> integerAL = new ArrayList<>();
List<Integer> integerLL = new LinkedList<>();
System.out.println("fill");
fill(integerAL, range);
fill(integerLL, range);
System.out.println("getIntegerAtIndex");
int al = getIntegerAtIndex(integerAL, range / 2);
int ll = getIntegerAtIndex(integerLL, range / 2);
System.out.println("AL: " + al + ", LL: " + ll);
}
static List fill(List list, int count)
{
long start = System.currentTimeMillis();
Random random = new Random();
while (count > 0) {
int nummber = random.nextInt(1000) + 1;
list.add(nummber);
count--;
}
long finisch = System.currentTimeMillis() - start;
System.out.println(finisch);
return list;
}
static int getIntegerAtIndex(List list, int index)
{
long start = System.currentTimeMillis();
int i = (int) list.get(index);
long finisch = System.currentTimeMillis() - start;
System.out.println(finisch);
return i;
}
}
/** Konsolenausgabe */
AL: [1]
AL: [1, 2]
LL: [1]
LL: [1, 2]
/** Also fuegen beide Listen mit der Methode add() die neuen Werte am Ende der Liste ein */
fill
30 //ArrayList deutlich schneller
109
getIntegerAtIndex
1 //ArrayList deutlich schneller
4
AL: 47, LL: 706 //Zufallswerte, nicht weiter beachten
[/code]