How to add string to string array
How to add string to string array
I’d like to convert the FI.Name to a string and then add it to my array. How can I do this?
15 Answers 15
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Alternatively, you can resize the array.
Use List from System.Collections.Generic
Or, shorthand (using collection initialiser):
If you really want an array at the end, use
You might be better off abstracting to an interface, such as IEnumerable, then just returning the collection.
Edit: If you must use an array, you can preallocate it to the right size (i.e. the number of FileInfo you have). Then, in the foreach loop, maintain a counter for the array index you need to update next.
If I’m not mistaken it is:
This is how I add to a string when needed:
Why don’t you use a for loop instead of using foreach. In this scenario, there is no way you can get the index of the current iteration of the foreach loop.
The name of the file can be added to the string[] in this way,
This code works great for preparing the dynamic values Array for spinner in Android:
add string to String array [duplicate]
I am new in Java so I need little help
and I want to add new strings ( string1,string2) to this array ( scripts) for examples
I want to add the new string not in the init but in later phase
5 Answers 5
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
You cannot resize an array in java.
Once the size of array is declared, it remains fixed.
Instead you can use ArrayList that has dynamic size, meaning you don’t need to worry about its size. If your array list is not big enough to accommodate new values then it will be resized automatically.
First, this code here,
Please read this tutorial on Arrays
Arrays are fixed size, so you can’t add new Strings to above array. You may override existing values
Create array with size then keep on adding elements till it is full.
If you want resizable arrays, consider using ArrayList.
you would have to write down some method to create a temporary array and then copy it like
or The ArrayList would be helpful to resolve your problem.
As many of the answer suggesting better solution is to use ArrayList. ArrayList size is not fixed and it is easily manageable.
It is resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list.
Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically.
Note that this implementation is not synchronized.
What is the most efficient way to add a string to this string array?
I have a string [] called myStrings. I can’t convert it to anything else, it has to stay a string array. However, I need to add one more string to it so I wrote this method. I am sure there is a better, faster, less memory intensive way to do this but I can’t see it. Can anyone provide a java api only way to solve this better than I have? I am using Java 1.7
3 Answers 3
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
You can’t add an item to a List returned by Arrays.asList(..) :
Returns a fixed-size list backed by the specified array. (Changes to the returned list «write through» to the array.) This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray().
You could use a separated List that you build from the array manually or use directly just Arrays :
If you absolutely have to use an Array then you could mimic what something like ArrayList does and double its size as needed. That way most inserts are very efficient (O(1)) complexity, but every once in a while you will have to do a full arraycopy of O(n) complexity.
This is bad design decision. If you need myStrings to change, then you need to declare it as a dynamic List from the beginning.
If you want to keep it as a fixed-bound array, then try to give it a size that you know will never be exceeded upon instantiation.
If you are unable to do that, you can use ArrayUtils.add(T[] array,T element)
The method copies your array and adds an item at the end. It should be faster than your algorithm, but not by much.
For all practical purposes, I wouldn’t worry about performance unless I see a bottleneck. For example, if you expect your array to contain 3-4 items, there is not point in worrying about this for the moment. Premature optimization is evil. 🙂
Add an item to a string[] array
I have class that has a property that is of type string[].
I need to create another array item, do I have to reset the size and copy all the previous items over?
8 Answers 8
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
You can use Array.Resize which will preserve the contents of the array.
You need to use a temporary variable because properties cannot be used as ref arguments.
Although it’s a best practice to not expose List as a property, so use IList or IEnumerable instead.
I would use a list if possible. It’s a lot easier to manage. Obviously this depends on if it’s your class and your able to chance this without any problems.
If your having to use arrays then use
Yes. Arrays have fixed size.
From C# specification:
12.2 Array creation
.
When an array instance is created, the rank and length of each dimension are established and then remain constant for the entire lifetime of the instance. In other words, it is not possible to change the rank of an existing array instance, nor is it possible to resize its dimensions.
You may also take a look at blog post from Eric Lippert Arrays considered somewhat harmful
Do you control the class? If so, consider using List internally instead.
Note that if you are doing this regularly, an extension method may be helpful:
Which you can then use for your property:
(note this works even if obj.SomeProp is null, creating an array of length 1)
If it were a field/variable, then a simple ref argument might also work:
How to dynamically add elements to String array? [closed]
I want to add dynamic number of elements to a string array from inside a for loop.
How to create an string array of an undefined length?
3 Answers 3
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Arrays in Java have a defined size, you cannot change it later by adding or removing elements (you can read some basics here).
Of course, if you know beforehand how many strings you are going to put in your array, you can create an array of that size and set the elements by using the correct position:
when using String array, you have to give size of array while initializing
you can use index 0-9 to store values
if you are using ArrayList instread of string array, you can use it without initializing size of array ArrayList str = new ArrayList();
you can add value by using add method of Arraylist
get retrieve a value from arraylist, you can use get method
find total number of items by size method
Источники информации:
- http://stackoverflow.com/questions/14098032/add-string-to-string-array
- http://stackoverflow.com/questions/19729568/what-is-the-most-efficient-way-to-add-a-string-to-this-string-array
- http://stackoverflow.com/questions/1520668/add-an-item-to-a-string-array
- http://stackoverflow.com/questions/15039519/how-to-dynamically-add-elements-to-string-array