# Square an arraylist (new to coding)

**URL:** <https://discourse.processing.org/t/square-an-arraylist-new-to-coding/15489>\
**Category:** Coding Questions\
**Created:** [November 14, 2019, 2:30pm UTC](https://discourse.processing.org/t/square-an-arraylist-new-to-coding/15489 "2019-11-14T14:30:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![NoSmoke11](https://avatars.discourse-cdn.com/v4/letter/n/a6a055/32.png) [@NoSmoke11](https://discourse.processing.org/u/NoSmoke11)\
**Post date:** [November 14, 2019, 2:30pm UTC](https://discourse.processing.org/t/square-an-arraylist-new-to-coding/15489/1 "2019-11-14T14:30:07Z")

</div>

Hello guys how do i square all the numbers in an arrayList?  
i want to square each number with itself, is this possible?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 14, 2019, 2:34pm UTC](https://discourse.processing.org/t/square-an-arraylist-new-to-coding/15489/2 "2019-11-14T14:34:23Z")

</div>

[https://processing.org/reference/sq\_.html](https://processing.org/reference/sq_.html)

please show your code with the loop over the arrayList

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 14, 2019, 3:41pm UTC](https://discourse.processing.org/t/square-an-arraylist-new-to-coding/15489/3 "2019-11-14T15:41:00Z")

</div>

Welcome to the forum!

Yes, you for loop over the arraylist and use get and set command - see the reference for Arraylist

---

<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:** [November 14, 2019, 9:37pm UTC](https://discourse.processing.org/t/square-an-arraylist-new-to-coding/15489/4 "2019-11-14T21:37:51Z")

</div>

[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html#replaceAll(java.util.function.UnaryOperator)](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html#replaceAll(java.util.function.UnaryOperator))

[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/function/UnaryOperator.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/function/UnaryOperator.html)

```java
// https://Discourse.Processing.org/t/square-an-arraylist-new-to-coding/15489/4
// GoToLoop (2019/Nov/14)

import java.util.List;
import java.util.function.UnaryOperator;

static final UnaryOperator<Float> SQ_FLOAT = new UnaryOperator<Float>() {
  @Override final Float apply(final Float n) {
    return sq(n);
  }
};

static final List<Float> sqList(final List<Float> list) {
  list.replaceAll(SQ_FLOAT);
  return list;
}

final List<Float> floats = new ArrayList<Float>();

void setup() {
  floats.add(PI);
  floats.add(TAU);
  floats.add(EPSILON);

  println(floats); // [3.1415927, 6.2831855, 1.0E-4]
  println(sqList(floats)); // [9.869605, 39.47842, 9.999999E-9]

  exit();
}

```

---

<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:** [November 14, 2019, 10:02pm UTC](https://discourse.processing.org/t/square-an-arraylist-new-to-coding/15489/5 "2019-11-14T22:02:17Z")

</div>

[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html#listIterator()](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html#listIterator())

[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/ListIterator.html#set(E)](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/ListIterator.html#set(E))

```java
// https://Discourse.Processing.org/t/square-an-arraylist-new-to-coding/15489/5
// GoToLoop (2019/Nov/14)

import java.util.List;
import java.util.ListIterator;

static final List<Float> sqList(final List<Float> list) {
  for (final ListIterator<Float> iter = list.listIterator(); iter.hasNext(); )
    iter.set(sq(iter.next()));
  return list;
}

final List<Float> floats = new ArrayList<Float>();

void setup() {
  floats.add(PI);
  floats.add(TAU);
  floats.add(EPSILON);

  println(floats); // [3.1415927, 6.2831855, 1.0E-4]
  println(sqList(floats)); // [9.869605, 39.47842, 9.999999E-9]

  exit();
}

```

---

<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:** [November 14, 2019, 10:16pm UTC](https://discourse.processing.org/t/square-an-arraylist-new-to-coding/15489/6 "2019-11-14T22:16:54Z")

</div>

[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html#set(int,E)](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html#set(int,E))

```java
// https://Discourse.Processing.org/t/square-an-arraylist-new-to-coding/15489/6
// GoToLoop (2019/Nov/14)

import java.util.List;

static final List<Float> sqList(final List<Float> list) {
  for (int i = 0, len = list.size(); i < len; ++i) list.set(i, sq(list.get(i)));
  return list;
}

final List<Float> floats = new ArrayList<Float>();

void setup() {
  floats.add(PI);
  floats.add(TAU);
  floats.add(EPSILON);

  println(floats); // [3.1415927, 6.2831855, 1.0E-4]
  println(sqList(floats)); // [9.869605, 39.47842, 9.999999E-9]

  exit();
}

```
