Guest User

Untitled

a guest
Jul 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. package beleg3;
  2.  
  3. import java.util.LinkedList;
  4. import java.util.Iterator;
  5. import java.util.Random;
  6.  
  7. public class Aufgabe2 {
  8. static LinkedList<Long> aList= new LinkedList();
  9. public static void main(String[] args) {
  10.  
  11. //Array anlegen mit Zufallszahlen
  12. long array[] = new long[20000001];
  13. Random rnd = new Random();
  14.  
  15.  
  16. for (int i = 0; i < array.length; i++) {
  17. long random=rnd.nextLong();
  18.  
  19. array[i] =random;
  20.  
  21. }
  22. long start = System.currentTimeMillis();
  23. suchFeld(array);
  24. long time = System.currentTimeMillis() - start;
  25. System.out.println("Zeit des Feldes:" + time);
  26.  
  27. long j=0;
  28. while(j<20000000){
  29. long random=rnd.nextLong();
  30. aList.add(random);
  31. j++;
  32. }
  33.  
  34. start = System.currentTimeMillis();
  35. suchListe(aList);
  36. time = System.currentTimeMillis() - start;
  37. System.out.println("Zeit der Liste " + time);
  38.  
  39. }
  40.  
  41. public static void suchFeld(long[] array) {
  42.  
  43. Random rnd = new Random();
  44. long gesucht =rnd.nextLong();
  45. System.out.println("Gesuchter Wert: "+ gesucht);
  46. boolean flag = false;
  47. for (int i = 0; i < array.length; i++) {
  48. if (gesucht == array[i]) {
  49. flag = true;
  50. gesucht=i;
  51. break;
  52. }
  53.  
  54. }
  55. if (flag == true) {
  56. System.out.println("An dieser Stelle gefunden: " + gesucht);
  57. }
  58. }
  59.  
  60. public static void suchListe(LinkedList<Long> aList) {
  61. Random rnd = new Random();
  62. long gesucht =rnd.nextLong();
  63. System.out.println("Gesuchter Wert: "+ gesucht);
  64. boolean flag = false;
  65.  
  66. for(long a:aList){
  67. if(a==gesucht){
  68. flag = true;
  69. gesucht=aList.get((int) a);
  70. break;
  71. }
  72. }
  73. if (flag == true) {
  74. System.out.println("An dieser Stelle gefunden: " + gesucht);
  75. }
  76. }
  77. }
Add Comment
Please, Sign In to add comment