Reputation: 2929
I tried use ArrayList in java
I want to do this like I do in array
String[] strArr=new String[10];
strArr[5]="bla bla";
when I do this
ArrayList<String> strArr=new ArrayList<String>()
strArr.add(5,"bla bla")
I cant do this cause this cell didnt already created. their is a way to do this?
Thanks
Upvotes: 0
Views: 142
Reputation: 143
Would this help you?
String[] strArr=new String[10];
strArr[5]="bla bla";
List<String> list = Arrays.asList(strArr);
The array will be mirrored into list 1:1 and the elements, which were not initialized in the array, will be set to null
in the list.
So the result of toString() method of the list will look like:
[null, null, null, null, null, bla bla, null, null, null, null]
Upvotes: 0
Reputation: 2830
Yes, it work as specified and implemented. The 10 (passed by constructor) is only "DEFAULT_CAPACITY" which only related by implementation functionality of class, but the logical size (size
property) is 0, and you can't add at 5-th index.
As you can see in implementation code of add()
method, it contains rangeCheckForAdd()
method
public void add(int index, E element) {
rangeCheckForAdd(index);
...
}
which checks index
with size
(not with DEFAULT_CAPACITY
which you provided as a constructor parameter)
private void rangeCheckForAdd(int index) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
And size
is the logical (contract) size of ArrayList on which you has no primary control.
And you can to increase the size only adding new elements to instance of ArrayList one after one:
strArr.add(0, "bla bla");
strArr.add(1, "bla bla 2");
strArr.add(2, "bla bla 3");
Or adding all at once, and then add 6-th (index 5)
strArr.addAll(Collections.nCopies(5, "empty")); // add 5 fake empty elements [0, 4]
strArr.add(5, "bla bla 6"); // Add at 5-th index the 6-th element
Upvotes: 0
Reputation: 93892
What you want is not possible using the ArrayList
class, because when you're calling add
or set
, it's checking that the index is not superior than the size. Since you didn't add any elements to your List
, you get the exception.
However there exists other List implementations that permits you to do this, like a GrowthList
.
This class avoids errors by growing when a set or add method would normally throw an IndexOutOfBoundsException. Note that IndexOutOfBoundsException IS returned for invalid negative indices.
Trying to set or add to an index larger than the size will cause the list to grow (using null elements). Clearly, care must be taken not to use excessively large indices, as the internal list will grow to match.
List<String> list = new GrowthList<>();
list.add(5, "hello"); //[null, null, null, null, null, hello]
Internally, this is how the method behave:
@Override
public void add(final int index, final E element) {
final int size = decorated().size();
if (index > size) {
decorated().addAll(Collections.<E>nCopies(index - size, null));
}
decorated().add(index, element);
}
As you can see, if the index is greater than the size, the underlying list is filled with null
values until the index you wanna add your element.
You could create your own ArrayList
implementation if you want, but if you can use a third party librairy, then there already exists some implementations.
Upvotes: 3
Reputation: 2135
List<String> strArr=new ArrayList<String>(Collections.nCopies(10, ""));
strArr.add(5,"bla bla");
System.out.println(strArr.toString());
Upvotes: 0