简体   繁体   English

需要有关解析 Java 中重复项的建议

[英]Need advice on parsing out duplicates in Java

I have an assignment that requires that I create two arrays (with user defined length) full of random numbers between 1-100, then merge the two arrays without duplicates in alternating order.我有一个任务,要求我创建两个数组(具有用户定义的长度),其中包含 1-100 之间的随机数,然后以交替顺序合并两个数组而没有重复。 I just need help merging the two without any repeating numbers.我只需要帮助将两者合并而没有任何重复的数字。 Can anyone help?任何人都可以帮忙吗? Thanks!谢谢!

import java.util.Scanner;
import java.lang.Math; 

class Main {

  public static void main(String[] args){
      Scanner scan = new Scanner (System.in);
      int f=0;
      int j=0;
      int k=0;
      //getting first array length and making array1 and array2 that length
    while (f==0){
      System.out.println("Enter an array length (must be 10 or greater):");
    int length = scan.nextInt();
    if (length < 10){
      f=0;
    }
    else{
      f=1;
    }
    if (f==1){
      int [] array1 = new int[length];
      int [] array2 = new int[length];
      int [] array3 = new int[length*2];
      System.out.print("First Array: ");
    //creating random integers between 1 and 100 inclusive to fill array1
    for (int i=0; i<=length-1; i++){
      int x = (int)(Math.random()*100)+1;
      array1[i] = x;
      System.out.print(x+" ");
    }
    //creating random integers between 1 and 100 inclusive to fill array2
    System.out.print("\nSecond Array: ");
    for (int i=0; i<=length-1; i++){
      int y = (int)(Math.random()*100)+1;
      System.out.print(y+" ");
      array2[i] = y;
    }
    //combining both arrays
    System.out.print("\nMerged Array: ");
    for (int i=0; i<=length*2-1; i++){
      if ((i==0) || (i%2==0)){
            array3[i] = array1[j];
            j++;      
          }
      else{
        array3[i] = array2[k];
        k++;
      }
      System.out.print(array3[i]+" ");
    }
    }
    }
  }
}

First, let's extract your method to fill arrays.首先,让我们提取填充数组的方法。

static int[] fillRandomArray(int n) {
    int[] arr = new int[n];
    for (int i = 0; i < n; i++) {
        arr[i] = (int) (Math.random() * 100) + 1;
        System.out.print(arr[i] + " ");
    }
    System.out.println();
    return arr;
}

Now you can simplify your code to use that method, and your merge is very close;现在您可以简化代码以使用该方法,并且您的合并非常接近; you don't need j or k in each case you are indexing by half of i (the cases being even or odd).在每种情况下,您都不需要jk索引i的一半(情况是偶数或奇数)。 Like,喜欢,

Scanner scan = new Scanner(System.in);
while (true) {
    System.out.println("Enter an array length (must be 10 or greater):");
    int length = scan.nextInt();
    if (length >= 10) {
        System.out.print("First Array: ");
        // creating random integers between 1 and 100 inclusive to fill array1
        int[] array1 = fillRandomArray(length);
        // creating random integers between 1 and 100 inclusive to fill array2
        System.out.print("\nSecond Array: ");
        int[] array2 = fillRandomArray(length);
        // combining both arrays
        System.out.print("\nMerged Array: ");
        int[] array3 = new int[array1.length + array2.length];
        for (int i = 0; i < array3.length; i++) {
            if (i % 2 == 0) {
                array3[i] = array1[i / 2];
            } else {
                array3[i] = array2[i / 2];
            }
            System.out.print(array3[i] + " ");
        }
        System.out.println();
    }
}

If you actually need to eliminate duplicates between array1 and array2 while you merge, then you can't assume that the output array will be double the input length.如果您在合并时确实需要消除 array1 和 array2 之间的重复项,那么您不能假设输出数组将是输入长度的两倍。 I would use a Set .我会使用Set Like,喜欢,

// combining both arrays
System.out.print("\nMerged Array: ");
Set<Integer> set = new LinkedHashSet<>();
for (int i = 0; i < array1.length + array2.length; i++) {
    if (i % 2 == 0) {
        if (set.add(array1[i / 2])) {
            System.out.print(array1[i / 2] + " ");
        }
    } else {
        if (set.add(array2[i / 2])) {
            System.out.print(array2[i] + " ");
        }
    }
}
// If you actually need an int[]
int[] array3 = set.stream().mapToInt(Integer::intValue).toArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM