我如何拍摄String[],并制作副本String[],但没有第一个字符串?示例:如果我有这个......
String[] colors = {"Red", "Orange", "Yellow"};
Run Code Online (Sandbox Code Playgroud)
我如何制作一个新的字符串,就像字符串集合颜色,但没有红色?
Pau*_*ora 14
你可以使用Arrays.copyOfRange:
String[] newArray = Arrays.copyOfRange(colors, 1, colors.length);
Run Code Online (Sandbox Code Playgroud)
忘了数组.它们不是初学者的概念.您可以更好地投入时间学习Collections API.
/* Populate your collection. */
Set<String> colors = new LinkedHashSet<>();
colors.add("Red");
colors.add("Orange");
colors.add("Yellow");
...
/* Later, create a copy and modify it. */
Set<String> noRed = new TreeSet<>(colors);
noRed.remove("Red");
/* Alternatively, remove the first element that was inserted. */
List<String> shorter = new ArrayList<>(colors);
shorter.remove(0);
Run Code Online (Sandbox Code Playgroud)
为了与基于阵列的遗留API互操作,有一个方便的方法Collections:
List<String> colors = new ArrayList<>();
String[] tmp = colorList.split(", ");
Collections.addAll(colors, tmp);
Run Code Online (Sandbox Code Playgroud)
String[] colors = {"Red", "Orange", "Yellow"};
String[] copy = new String[colors.length - 1];
System.arraycopy(colors, 1, copy, 0, colors.length - 1);
Run Code Online (Sandbox Code Playgroud)