-6

是否可以制作数组的数组?我要做的基本上是制作一个没有重复的另一个数组的数组。

例如:

String[] colour ={"blue","blue","red","blue","red","red","orange","yellow","purple","green","blue"};

然后从 String[] color 创建一个新数组:

String[] uniqueColour = {"blue","red","orange","yellow","purple","green"}

使用函数而不只是声明它?原因可以说我更改了 String[] Color 的所有值并将其变成

String[] Fruits = {"Apple","Banana","Orange","Tomato","Apple","Banana"}

然后不做或改变任何其他功能应该创建

String[] uniqueColour ={"Apple","Banana","Orange","Tomato"}

这样的事情存在吗?抱歉,添麻烦了。我也是java新手。

感谢任何可以贡献或帮助我的人。

编辑:好的,使用这个: - 感谢Doorknob的回答

Set<String> uniqueSet = new HashSet<String>(Arrays.asList(colour));
String[] uniqueColours = uniqueSet.toArray(new String[0]);

然后我将如何尝试将其显示为:

Blue
Red
Yellow
Green
Purple
ect

代替[Blue,Red,Yellow,Green,Purple,etc]

4

1 回答 1

1

使用 aSet删除重复项:

Set<String> uniqueSet = new HashSet<String>(Arrays.asList(colour));
String[] uniqueColours = uniqueSet.toArray(new String[0]);

要以您想要的方式显示它:

for (String s : uniqueColors) System.out.println(s);
于 2013-04-18T12:15:52.197 回答