2016-05-16 72 views
-1

我想從陣列輸出在JList中刪除項目刪除陣列的jList特定元素上面的代碼

String[] cdList = {"Adele", "Drake", "Prince", "Rihanna", "Sia"} ; 

輸出

String[] cdList = {"Adele", "Drake", "Prince", "Sia"} ; 
+1

數組有固定長度。你可能想要一個'ArrayList '。你也可以複製一個長度爲1的數組,並跳過你想要移除的元素。 – 4castle

+0

您是否嘗試過使用for循環進行迭代,並檢查它是否匹配每個週期? –

+0

您可以提供一些有關陣列的細節以及您想要做什麼?數組是否故意排序?你是通過索引還是按價值去除?是否有重複的項目被刪除,並且你想刪除所有發生的事件?你的問題很不清楚。 – 4castle

回答

1

這會幫助你:

List<String> list = new ArrayList<String>(Arrays.asList(cdList)); 
list.remove(cdList[i]); 
cdList = list.toArray(new String[0]); 

我已經寫成這樣,享受!

String[] cdList = {"Adele", "Drake", "Prince", "Rihanna", "Sia"} ; 

for(int i = 0; i< cdList.length;i++){ 
    System.out.println(i+1 + " . " + cdList[i]); 
} 
System.out.println("Enter the number of the name you want to remove:"); 
Scanner scan = new Scanner(System.in); 
int num = scan.nextInt(); 
List<String> list = new ArrayList<String>(Arrays.asList(cdList)); 
list.remove(cdList[num-1]); 
cdList = list.toArray(new String[0]); 
for(int i = 0; i< cdList.length;i++){ 
    System.out.println(i+1 + " . " + cdList[i]); 
} 

乾杯!