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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26679860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:23:30  来源:igfitidea点击:

Pulling distinct values from a array in java

javaarraysunique

提问by Chewy

Have a program where the user inputs 10 int values into the array. Lastly I need to pull out the distinct values and display them. Added my second for loop which would determine if the the value is distinct (i.e. meaning if the number appears multiple times it is only displayed once).

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

For instance, let say I pass in the numbers: 1, 2, 3, 2, 1, 6, 3, 4, 5, 2 the distinct array should only contain numbers {1, 2, 3, 6, 4, 5}

例如,假设我传入数字: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

回答by Kick Buttowski

In Java 8

Java 8

Stream< T > distinct()

流<T>distinct()

Returns a stream consisting of the distinct elements (according to Object.equals(Object)) of this stream. For ordered streams, the selection of distinct elements is stable (for duplicated elements, the element appearing first in the encounter order is preserved.) For unordered streams, no stability guarantees are made.

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

Code:

代码:

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

Output:

输出:

5,10,20,58

Read More About distinct function

阅读有关不同功能的更多信息

回答by ToYonos

Try this :

试试这个 :

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

uniqueNumberswill contain only unique values

uniqueNumbers将只包含唯一值

回答by Anas K

Try this code.. it will work

试试这个代码..它会工作

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] + " ");
    }
}

回答by geonz

One possible logic: If you're supposed to only sort out "unique" numbers, then you'll want to test each number as it's entered and added to the first array, and loop through the array and see if it's equal to any of the numbers already there; if not, add it to the "unique" array.

一个可能的逻辑:如果你应该只整理出“唯一”的数字,那么你会想要测试每个数字,因为它被输入并添加到第一个数组中,并循环遍历数组,看看它是否等于任何一个已经存在的数字;如果没有,请将其添加到“唯一”数组中。

回答by Paulius Matulionis

Sets in java doesn't allow duplicates:

java中的集合不允许重复:

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

That's it.

而已。

回答by brso05

Something like this should work for you:

像这样的事情应该适合你:

     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);
     }

This will only add unique values to the set.

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

回答by Lilaram Anjane

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]);
    }
}