2014-10-31 27 views
0

有一個程序,用戶在該數組中輸入10個int值。最後,我需要提取不同的值並顯示它們。增加了我的第二個循環,用於確定值是否不同(即表示該數字多次出現,只顯示一次)。例如,假設我傳入數字:1,2,3,2,1,6,3,4,5,2,不同的數組只應包含數字{1,2,3,4,5,2} 4,5}從java中的數組中提取不同的值

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 
+1

什麼是 「獨特」 的價​​值觀?我認爲你是在假設我們有一些先前的知識。請更好地解釋你的問題。 – markspace 2014-10-31 17:02:58

+2

考慮使用一套只允許唯一的值... – brso05 2014-10-31 17:03:03

+1

@ brso05我猜它是作業;他還沒有設置。 – markspace 2014-10-31 17:04:10

回答

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

它將只存儲集合中的唯一值。 – brso05 2014-10-31 17:09:08

3

嘗試這種情況:

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

uniqueNumbers將只包含唯一值

+0

如果我不想對數據進行排序會怎樣? – 2016-03-04 07:03:21

3

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 

Read More About distinct function

+0

@Chewy看看這個鏈接更多信息http://stackoverflow.com/questions/17967114/how-to-remove-duplicates-from-an-array-in-java – 2014-10-31 17:32:19

1

一個可能的邏輯是:如果你應該只整理出 「獨一無二」 的號碼,那麼你」我們希望測試輸入的每個數字並將其添加到第一個數組中,然後遍歷數組並查看它是否與n中的任何一個相等議員們已經在那裏;如果沒有,請將其添加到「唯一」數組中。

+0

編輯我的原始帖子與邏輯你說...如果這是不正確的,可以解釋我可以如何重寫,而不給我答案 – Chewy 2014-10-31 18:45:10

+0

@Chewy我對我的答案發表評論去那裏它可以幫助你 – 2014-10-31 20:44:00

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

你可以擴大你的答案,包括解釋爲什麼這是對OP的要求有幫助嗎? – ManoDestra 2016-07-18 17:34:54