Java向字符串数组添加变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21229409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java add variable to string array
提问by
I have a small code that includes citynames wich will be displayed. Now a want a user can add names with a scanner, I know the code for the sanner but not how to add the variable.
我有一个小代码,其中包含将显示的城市名称。现在希望用户可以使用扫描仪添加名称,我知道 sanner 的代码,但不知道如何添加变量。
Code i have:
我有代码:
String[] cityNames = { "Tiel", "Culemborg", "Houten", "Geldermalsen", "Meteren", "Buren" };
System.out.println(Arrays.toString(cityNames));
回答by Suresh Atta
No you cannot do it with a Array since the size is fixed , once it declared.
不,你不能用数组来做,因为大小是固定的,一旦声明。
You are probably looking for Collections. Prefer to Use List
interface with ArrayList
implementation.
您可能正在寻找收藏。更喜欢使用 List
带有ArrayList
实现的接口。
The reason is that the ArrayList
is
原因ArrayList
是
Resizable-array implementation of the List interface.
List 接口的可调整大小的数组实现。
List<String> cityNames = new ArrayList<>();
Now you have methods like add, remove
, ... and many more useful methods on your cityNames
List
现在您拥有诸如add, remove
, ... 之类的方法以及更多有用的方法cityNames
List
回答by BobTheBuilder
You can use a List<String>
, get the input value and add it:
您可以使用 a List<String>
,获取输入值并添加它:
List<String> cities = new ArrayList<>();
cities.add(userInput);
List is better to use than array as its length is modifiable.
列表比数组更好用,因为它的长度是可以修改的。
回答by Jannis Alexakis
Arrays have a fixed length. If the amount of Strings in your collection is variable, you′ll have to use a List.
数组具有固定长度。如果集合中的字符串数量是可变的,则必须使用列表。
回答by Ruchira Gayan Ranaweera
You can add new element to array if index of new element less than the size of array.
如果新元素的索引小于数组的大小,则可以向数组添加新元素。
arr[i]="some value" // to do this i < arr.length
If array is completely filled with elements when you assign new value to index previous value will override. You can't add more elements than the size of declared since array has fixed size.
如果在为索引分配新值时数组完全充满元素,则先前的值将被覆盖。您不能添加超过声明大小的元素,因为数组具有固定大小。
回答by Prabhakaran Ramaswamy
Array is fixed size so you can't add the value to it if the size is already filled. For dynamic array use List instead of array.
数组是固定大小,因此如果大小已经填充,则无法向其添加值。对于动态数组,请使用 List 而不是数组。
Do like this
这样做
List<String> list = new ArrayList<String>(Arrays.asList("Tiel", "Culemborg", "Houten", "Geldermalsen", "Meteren", "Buren" ));
list.add("new value1");
list.add("new value2");
回答by Autocrab
It's better to use there set, which excludes duplicate entries automatically:
最好使用那里设置,它会自动排除重复条目:
Set<String> cities = new HashSet<String>();
cities.addAll(Arrays.asList("Tiel", "Culemborg", "Houten", "Geldermalsen", "Meteren", "Buren"));
then to add new city just call:
然后添加新城市只需调用:
sities.add(newCity);
回答by Smartian
Scanner input = new Scanner(System.in);
List<String> cityNames = new ArrayList<String>();
//Add the city names to cityNames list...
cityNames.add(input.next());