Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. package java1920;
  2.  
  3. import java.util.Scanner;
  4. import java.util.Arrays;
  5. import java.lang.Math;
  6.  
  7. public class Lesson_0_Activity_0 {
  8. public static void main(String[]arg) {
  9. Scanner kb = new Scanner (System.in);
  10.  
  11. System.out.println("Enter an array length (must be 10 or greater): ");
  12. int arrLength = kb.nextInt();
  13.  
  14. if (arrLength < 10) {
  15. while (arrLength < 10) {
  16. System.out.println("Enter an array length (must be 10 or greater): ");
  17. arrLength = kb.nextInt();
  18. }
  19. }
  20.  
  21. int[]list1 = new int [arrLength];
  22. int[]list2 = new int [arrLength];
  23.  
  24. for (int i = 0; i < arrLength; i++) {
  25. list1[i] = (int)(Math.random()*100+1);
  26. for (int j = 0; j < arrLength; j++) {
  27. list2[j] = (int)(Math.random()*100+1);
  28. }
  29. }
  30.  
  31. int[]list = new int [arrLength*2];
  32.  
  33. int c = 0;
  34.  
  35. for (int i = 0; i < arrLength*2; i++) {
  36. if (i % 2 == 0)
  37. list[i] = list1[c];
  38. else
  39. c++;
  40. }
  41.  
  42. c = 0;
  43.  
  44. for (int i = 1; i < arrLength*2; i+=2) {
  45. list[i] = list2[c];
  46. c++;
  47. }
  48.  
  49. c = 0;
  50.  
  51. for (int i = 0; i < arrLength*2; i++) {
  52. for (int j = i + 1; j < arrLength*2; j++) {
  53. if (list[i] == list[j]) {
  54. list[j] = 0;
  55. }
  56. }
  57. }
  58.  
  59. int arrLengthC = arrLength * 2;
  60.  
  61. for (int i = 0; i < arrLengthC; i++) {
  62. if (list[i] == 0)
  63. arrLengthC --;
  64. }
  65.  
  66. int[]listC = new int [arrLengthC];
  67.  
  68. c = 0;
  69.  
  70. for (int i = 0; i < list.length; i++) {
  71. if (list[i] != 0) {
  72. listC[c] = list[i];
  73. c++;
  74. }
  75. }
  76.  
  77. System.out.println();
  78. System.out.print("First Array: ");
  79. for (int i = 0; i < arrLength; i++) {
  80. System.out.print(list1[i] + " ");
  81. }
  82. System.out.println();
  83. System.out.println();
  84. System.out.print("Second Array: ");
  85. for (int i = 0; i < arrLength; i++) {
  86. System.out.print(list2[i] + " ");
  87. }
  88. System.out.println();
  89. System.out.println();
  90. System.out.print("Merged Array: ");
  91. for (int i = 0; i < arrLengthC; i++) {
  92. if (listC[i] == 0)
  93. c = c;
  94. else
  95. System.out.print(listC[i] + " ");
  96. }
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement