# Array expand doesn't always work?

**URL:** <https://discourse.processing.org/t/array-expand-doesnt-always-work/10410>\
**Category:** Beginners\
**Created:** [April 17, 2019, 8:39pm UTC](https://discourse.processing.org/t/array-expand-doesnt-always-work/10410 "2019-04-17T20:39:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)\
**Post date:** [April 17, 2019, 8:39pm UTC](https://discourse.processing.org/t/array-expand-doesnt-always-work/10410/1 "2019-04-17T20:39:55Z")

</div>

The example given for ‘expand’ works alright, but the code below is the example for loadStrings, with expand added. Why doesn’t it expand the array?

```auto
String[] lines = loadStrings("list.txt");
println("there are " + lines.length + " lines");
for (int i = 0 ; i < lines.length; i++) {
  println(lines[i]);
}
println(lines.length); 
expand(lines, 20);
println(lines.length); 

```

Output:

> there are 5 lines  
> apple  
> orange  
> banana  
> pear  
> apricot  
> 5  
> 5

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 17, 2019, 8:54pm UTC](https://discourse.processing.org/t/array-expand-doesnt-always-work/10410/2 "2019-04-17T20:54:00Z")

</div>

Java [arrays](https://processing.org/reference/Array.html) can’t have its _length_ changed once created. ☕

What [**expand()**](https://processing.org/reference/expand_.html) and other similar functions do is create another array w/ a diff. _length_, copy the contents of the original array to the new 1, and then return the new 1. 🧐

So we do need to store the newly returned array to some variable. 😳

That’s why an ArrayList, or in your particular case a StringList, is a much better choice as a dynamic _length_ container: 💡

1. [Processing.org/reference/ArrayList.html](http://Processing.org/reference/ArrayList.html)
2. [Processing.org/reference/StringList.html](http://Processing.org/reference/StringList.html)
3. [Forum.Processing.org/two/discussion/8080/why-use-arraylist-instead-of-array-with-append](http://Forum.Processing.org/two/discussion/8080/why-use-arraylist-instead-of-array-with-append)

---

<div class="post-metadata">

**Author:** ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)\
**Post date:** [April 17, 2019, 9:05pm UTC](https://discourse.processing.org/t/array-expand-doesnt-always-work/10410/3 "2019-04-17T21:05:47Z")

</div>

Yes, thanks, just realised I should have ’ lines = expand(lines, 20); ’

I’ve used ArrayList lots of times, it’s very good.
