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

问题描述 投票:0回答:8

有一个程序,用户将 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
java arrays unique
8个回答
5
投票

Java 8

流< 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

了解更多关于不同功能的信息


4
投票

试试这个:

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

uniqueNumbers
将仅包含唯一值


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

1
投票

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


0
投票

java中的集合不允许重复:

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

就是这样。


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

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


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

0
投票

您可以使用stream(),然后使用distinct(),这将从传递的int数组中删除所有重复值。

代码:

    int numbers [] = {1, 2, 3, 2, 1, 6, 3, 4, 5, 2};
    List<Integer> distinctNumbers = Arrays.stream(numbers).boxed().distinct().sorted().collect(Collectors.toList());
    distinctNumbers.stream().forEach(s-> System.out.println(s));
© www.soinside.com 2019 - 2024. All rights reserved.