# StringList Manipulation

**URL:** <https://discourse.processing.org/t/stringlist-manipulation/1561>\
**Category:** Coding Questions\
**Created:** [July 6, 2018, 8:44pm UTC](https://discourse.processing.org/t/stringlist-manipulation/1561 "2018-07-06T20:44:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![poltergeister](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/poltergeister/32/648_2.png) [@poltergeister](https://discourse.processing.org/u/poltergeister)\
**Post date:** [July 6, 2018, 8:44pm UTC](https://discourse.processing.org/t/stringlist-manipulation/1561/1 "2018-07-06T20:44:12Z")

</div>

I’m trying to manipulate the contents of a StringList for use in one place in the code while leaving the original list intact. I’m finding this doesn’t work. I’m guessing that the StringList is being maintained by ‘reference’ somehow and when I tweak the copy it changes the original as well. Thoughts on how to force Processing to create new StringList object that isn’t ‘linked’

see code snippet:  
it removes the first entry ("") and adds empty ("") last entry.

```auto
StringList tweakList(StringList orig) {
  StringList result = new StringList();
  result = orig;
  result.append("");
  result.remove(0);
  return result;
}

```

```auto
  List lst = Arrays.asList(tweakList(imgAttributes).array());
  println(".........");  
  println("Attributes 'lst' list: " + lst);
  println(".........");  
  print("imgAttributes after tweekList() " );
  printArray(imgAttributes);

```

This is what I see in the results console  
(first result below is from an earlier ‘print’ call)

```auto
imgAttributes file at build..........
StringList size=9 ["", "bob", "phil", "anni", "may", "fido", "rover", "meow", "puss"]
.........
Attributes 'lst' list: [bob, phil, anni, may, fido, rover, meow, puss,]
.........
imgAttributes after tweekList() StringList size=9 ["bob", "phil", "anni", "may", "fido", "rover", "meow", "puss", ""]

```

The entry is moved as needed, but the the underlying StringList “imgAttributes” is modified… even though I’ve not worked on it, at least ‘directly’.  
Is there another operator other than ‘=’ that I should be using in ‘result = orig;’ assignment?

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [July 6, 2018, 10:02pm UTC](https://discourse.processing.org/t/stringlist-manipulation/1561/2 "2018-07-06T22:02:41Z")

</div>

> [@poltergeister](#):
>
> ```
> StringList result = new StringList(); 
> result = orig;
> 
> ```

Note that your first line of code here has no effect on whether the list is a copy or not. You’re basically saying “point `result` to a new list, nevermind point it to the parameter list”.

To understand what’s going on better, I recommend reading this:

> **[JavaRanch Campfire - Cup Size: a Story About Variables](https://javaranch.com/campfire/StoryCups.jsp)**
>
> Leasons for learning the basics of Java Programming

And then this:

> **[JavaRanch Campfire - Pass By Value, Please](https://javaranch.com/campfire/StoryPassBy.jsp)**
>
> Leasons for learning the basics of Java Programming

Anyway, the best way to answer questions like this is by looking at [the Processing JavaDocs](http://processing.github.io/processing-javadocs/core/). Find the `StringList` class and look for useful functions. Specifically, it contains a `copy()` function that sounds like what you’re looking for.

---

<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:** [July 6, 2018, 11:03pm UTC](https://discourse.processing.org/t/stringlist-manipulation/1561/3 "2018-07-06T23:03:17Z")

</div>

Some cloning insights: 😀

> **[Final array is being changed](https://Discourse.Processing.org/t/final-array-is-being-changed/1447/6?u=gotoloop)**
>
> The 1st example explains at its comments why the 2nd 1 works. 😉 Processing got nothing to do w/ that, but the Java language itself! ☕ As you know by now, the 1st example already got those basic insights. 🙄 Basically, we can have more than 1...

---

<div class="post-metadata">

**Author:** ![poltergeister](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/poltergeister/32/648_2.png) [@poltergeister](https://discourse.processing.org/u/poltergeister)\
**Post date:** [July 7, 2018, 11:44pm UTC](https://discourse.processing.org/t/stringlist-manipulation/1561/4 "2018-07-07T23:44:48Z")

</div>

Thanks  
replacing

```auto
StringList result = new StringList(); 
result = orig;

```

with

```auto
StringList result = orig.copy();

```

did the trick.  
Populates the new StringList with the contents of the original (not a pointer to original)
