从java中的数组中提取不同的值

时间:2014-10-31 17:01:25

标签: java arrays unique

有一个程序,用户在数组中输入10个int值。最后,我需要提取不同的值并显示它们。添加了我的第二个for循环,它将确定该值是否是不同的(即,如果该数字出现多次,则表示仅显示一次)。

例如,假设我传入数字:1,2,3,2,1,6,3,4,5,2这个不同的数组应该只包含数字{1,2,3,6,4 ,5}

import java.util.Scanner;
import java.io.*;

public class ArrayDistinct {
 public static void main(String[] args) throws IOException {

 Scanner input = new Scanner(System.in);

 // Create arrays & variables  
 int arrayLength = 10;
 int[] numbers = new int[arrayLength];
 int[] distinctArray = new int[arrayLength];
 int count = 0;

 System.out.println("Program starting...");
 System.out.print("Please enter in " + numbers.length + " numbers: ");

 for (int i = 0; i < numbers.length; i++) {
  numbers[i] = input.nextInt();
 }

 for (int i = 0; i < numbers.length; i++) {
  int temp = numbers[i];
  int tempTwo = numbers[i + 1];

  if (tempTwo == temp) {
   count++;
   distinctArray[i] = temp;
  }
 } 

 // Print out results

} // end main
} // end class

7 个答案:

答案 0 :(得分:3)

试试这个:

Set<Integer> uniqueNumbers = new HashSet<Integer>(Arrays.asList(numbers));

uniqueNumbers将仅包含唯一值

答案 1 :(得分:3)

Java 8

<强>流&LT; T>不同()

  

返回由不同元素组成的流(根据   此流的Object.equals(Object))。对于有序流,   选择不同的元素是稳定的(对于重复的元素,   在遇到顺序中首先出现的元素被保留。)对于   无序流,没有稳定性保证。

代码:

   Integer[] array = new Integer[]{5, 10, 20, 58, 10};
   Stream.of(array)
         .distinct()
         .forEach(i -> System.out.print(" " + i));

输出:

5,10,20,58

Read More About distinct function

答案 2 :(得分:2)

试试这段代码..它会起作用

package Exercises;
import java.util.Scanner;
public class E5Second
{
    public static void main(String[] args) 
    {
        Scanner In = new Scanner(System.in);
        int [] number = new int [10];
        fillArr(number);

        boolean [] distinct = new boolean [10];

        int count = 0; 
        for (int i = 0; i < number.length; i++) 
        {
            if (isThere(number,i) == false)
            {
                distinct[i] = true;
                count++;
            }
        }
        System.out.println("\nThe number of distinct numbers is  " + count);
        System.out.print("The distinct numbers are: ");
        displayDistinct(number, distinct);
    }
    public static void fillArr(int [] number)
    {
        Scanner In = new Scanner(System.in);
        System.out.print("Enter ten integers ");
        for (int i = 0; i < number.length; i++)
            number[i] = In.nextInt();
    }
    public static boolean isThere(int [] number, int i)
    {
        for (int j = 0; j < i; j++)
            if(number[i] == number[j])
                return true;
        return false;
    }
    public static void  displayDistinct(int [] number, boolean [] distinct)
    {
        for (int i = 0; i < distinct.length; i++)
            if (distinct[i]) 
                System.out.print(number[i] + " ");
    }
}

答案 3 :(得分:1)

一个可能的逻辑:如果您只是要排序“唯一”数字,那么您将需要测试每个数字,因为它已输入并添加到第一个数组中,并循环遍历数组并查看它是否相等已经存在的任何数字;如果没有,请将其添加到“唯一”数组。

答案 4 :(得分:0)

java中的集合不允许重复:

    Integer[] array = new Integer[]{5, 10, 20, 58, 10};
    HashSet<Integer> uniques = new HashSet<>(Arrays.asList(array));

那就是它。

答案 5 :(得分:0)

这样的事情对你有用:

     Scanner input = new Scanner(System.in);

     // Create arrays & variables  
     int arrayLength = 10;
     int[] numbers = new int[arrayLength];
     int[] distinctArray = new int[arrayLength];
     int count = 0;
     Set<Integer> set = new HashSet<Integer>();

     System.out.println("Program starting...");
     System.out.print("Please enter in " + numbers.length + " numbers: ");

     for (int i = 0; i < numbers.length; i++) {
         set.add(input.nextInt());
     }

     for(Integer i : set)
     {
         System.out.println("" + i);
     }

这只会为集合添加唯一值。

答案 6 :(得分:0)

int a[] = { 2, 4, 5, 3, 3, 3, 4, 6 };
int flag = 0;
for (int i = 0; i < a.length; i++)
{  
    flag = 0;
    for (int j = i + 1; j < a.length; j++)
    {
        if (a[i] == a[j])
        {
            flag = 1;
        }
    }

    if (flag == 0)
    {
        System.out.println(a[i]);
    }
}