从代码

时间:2016-05-16 01:03:59

标签: java

我想从jlist

中的数组输出中删除一个项目
String[] cdList = {"Adele", "Drake", "Prince", "Rihanna", "Sia"}  ;

输出

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

1 个答案:

答案 0 :(得分: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]);
}

喝彩!