JAVA使用下面的方法頭編寫方法,從一個整數的數組列表中去掉重復的元素


 

 

import java.util.Scanner;
public class GAGA{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
//提示輸入
System.out.print("Enter ten numbers: ");
int[] a = new int[10];
for(int i = 0; i < 10; i++)
{
a[i] = input.nextInt();
}
//篩選
System.out.print("The distinct numbers are: ");
int[] b = {1,1,1,1,1,1,1,1,1,1};
for(int i = 0; i < 10; i++)
{
for(int j = 9; j > i; j--)
{
if(a[i] == a[j])
{
b[j] = -1;
}
}
}
//輸出
for(int i = 0; i < 10; i++)
{
if(b[i] == 1)
{
System.out.print(a[i] +" ");
}
}
}
}

輸出結果就這:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM