Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 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.  
  32.  
  33. int[]list = new int [arrLength*2];
  34.  
  35. int c = 0;
  36.  
  37. for (int i = 0; i < arrLength*2; i++) {
  38. if (i % 2 == 0)
  39. list[i] = list1[c];
  40. else
  41. c++;
  42. }
  43.  
  44. c = 0;
  45.  
  46. for (int i = 1; i < arrLength*2; i+=2) {
  47. list[i] = list2[c];
  48. c++;
  49. }
  50.  
  51. c = 0;
  52.  
  53. for (int i = 0; i < arrLength*2; i++) {
  54. for (int j = i + 1; j < arrLength*2; j++) {
  55. if (list[i] == list[j]) {
  56. list[j] = 0;
  57. }
  58. }
  59. }
  60.  
  61. int arrLengthC = arrLength * 2;
  62.  
  63. for (int i = 0; i < arrLengthC; i++) {
  64. if (list[i] == 0)
  65. arrLengthC --;
  66. }
  67.  
  68. int[]listC = new int [arrLengthC];
  69.  
  70. c = 0;
  71.  
  72. for (int i = 0; i < list.length; i++) {
  73. if (list[i] != 0) {
  74. listC[c] = list[i];
  75. c++;
  76. }
  77. }
  78.  
  79. System.out.println();
  80. System.out.print("First Array: ");
  81. for (int i = 0; i < arrLength; i++) {
  82. System.out.print(list1[i] + " ");
  83. }
  84. System.out.println();
  85. System.out.println();
  86. System.out.print("Second Array: ");
  87. for (int i = 0; i < arrLength; i++) {
  88. System.out.print(list2[i] + " ");
  89. }
  90. System.out.println();
  91. System.out.println();
  92. System.out.print("Merged Array: ");
  93. for (int i = 0; i < arrLengthC; i++) {
  94. if (listC[i] == 0)
  95. c = c;
  96. else
  97. System.out.print(listC[i] + " ");
  98. }
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement