Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package beleg3;
- import java.util.LinkedList;
- import java.util.Iterator;
- import java.util.Random;
- public class Aufgabe2 {
- static LinkedList<Long> aList= new LinkedList();
- public static void main(String[] args) {
- //Array anlegen mit Zufallszahlen
- long array[] = new long[20000001];
- Random rnd = new Random();
- for (int i = 0; i < array.length; i++) {
- long random=rnd.nextLong();
- array[i] =random;
- }
- long start = System.currentTimeMillis();
- suchFeld(array);
- long time = System.currentTimeMillis() - start;
- System.out.println("Zeit des Feldes:" + time);
- long j=0;
- while(j<20000000){
- long random=rnd.nextLong();
- aList.add(random);
- j++;
- }
- start = System.currentTimeMillis();
- suchListe(aList);
- time = System.currentTimeMillis() - start;
- System.out.println("Zeit der Liste " + time);
- }
- public static void suchFeld(long[] array) {
- Random rnd = new Random();
- long gesucht =rnd.nextLong();
- System.out.println("Gesuchter Wert: "+ gesucht);
- boolean flag = false;
- for (int i = 0; i < array.length; i++) {
- if (gesucht == array[i]) {
- flag = true;
- gesucht=i;
- break;
- }
- }
- if (flag == true) {
- System.out.println("An dieser Stelle gefunden: " + gesucht);
- }
- }
- public static void suchListe(LinkedList<Long> aList) {
- Random rnd = new Random();
- long gesucht =rnd.nextLong();
- System.out.println("Gesuchter Wert: "+ gesucht);
- boolean flag = false;
- for(long a:aList){
- if(a==gesucht){
- flag = true;
- gesucht=aList.get((int) a);
- break;
- }
- }
- if (flag == true) {
- System.out.println("An dieser Stelle gefunden: " + gesucht);
- }
- }
- }
Add Comment
Please, Sign In to add comment