# Mixed Datatype Arrays

**URL:** <https://discourse.processing.org/t/mixed-datatype-arrays/14673>\
**Category:** Beginners\
**Created:** [October 13, 2019, 10:32pm UTC](https://discourse.processing.org/t/mixed-datatype-arrays/14673 "2019-10-13T22:32:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![KeirMeDear](https://avatars.discourse-cdn.com/v4/letter/k/8c91f0/32.png) [@KeirMeDear](https://discourse.processing.org/u/KeirMeDear)\
**Post date:** [October 13, 2019, 10:32pm UTC](https://discourse.processing.org/t/mixed-datatype-arrays/14673/1 "2019-10-13T22:32:27Z")

</div>

Is there a way to have various datatypes in one array? Checking online it says to initialise as an Object type but that doesn’t seem to work with processing. Can’t seem to find anything in the docu either

The Goal:

datatype[][] arrayA = {“hello”,1,2,3.5};

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [October 13, 2019, 11:38pm UTC](https://discourse.processing.org/t/mixed-datatype-arrays/14673/2 "2019-10-13T23:38:01Z")

</div>

Being a strong typed language based on java processing doesnt allow multiple types in one array, there are alternatives though not as easily implemented as in a language such as javascript.

[https://www.techiedelight.com/return-multiple-values-method-java/](https://www.techiedelight.com/return-multiple-values-method-java/)

---

<div class="post-metadata">

**Author:** ![KeirMeDear](https://avatars.discourse-cdn.com/v4/letter/k/8c91f0/32.png) [@KeirMeDear](https://discourse.processing.org/u/KeirMeDear)\
**Post date:** [October 13, 2019, 11:39pm UTC](https://discourse.processing.org/t/mixed-datatype-arrays/14673/3 "2019-10-13T23:39:46Z")

</div>

> [@paulgoux](#):
>
> [Return multiple values from a method in Java | Techie Delight](https://www.techiedelight.com/return-multiple-values-method-java/)

Ah that’s frustrating, coming from Javascript it’s annoying you can’t do that - Many thanks though!

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [October 13, 2019, 11:39pm UTC](https://discourse.processing.org/t/mixed-datatype-arrays/14673/4 "2019-10-13T23:39:51Z")

</div>

Essentially you have to create a separate class to handle your multiple type data, you can then create an array or array list with the type defined by your class, and you can then call the values.

---

<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:** [October 13, 2019, 11:43pm UTC](https://discourse.processing.org/t/mixed-datatype-arrays/14673/5 "2019-10-13T23:43:05Z")

</div>

> [@KeirMeDear](#):
>
> Is there a way to have various datatypes in one array?

- The only way in pure Java is to go w/ datatype [Object](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html).
- However, in order to read each element from such container, we need to `(cast)` each element back to its actual datatype.
- Which of course is not very practical; and therefore, not recommended.
- The alternative is to create a [`class`](https://processing.org/reference/class.html) containing all the needed datatypes as fields.

```auto
// https://Discourse.Processing.org/t/mixed-datatype-arrays/14673/5
// GoToLoop (2019-Oct-13)

final Mixed[] mix = {
  new Mixed("hello", 1, 2, 3.5), 
  new Mixed("mixed", -1, -2, -3.5), 
  new Mixed("types", MAX_INT, MIN_INT, 1e-3)
};

void setup() {
  printArray(mix);
  exit();
}

class Mixed {
  String txt;
  int a, b;
  float f;

  Mixed(String txt, int a, int b, float f) {
    this.txt = txt;
    this.a = a;
    this.b = b;
    this.f = f;
  }

  String toString() {
    return txt + ", " + a + ", " + b + ", " + f;
  }
}

```

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [October 13, 2019, 11:43pm UTC](https://discourse.processing.org/t/mixed-datatype-arrays/14673/6 "2019-10-13T23:43:21Z")

</div>

I know the feeling, things that are so easy in JavaScript, require so much depth of understanding about the java language to implement in java.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [October 13, 2019, 11:44pm UTC](https://discourse.processing.org/t/mixed-datatype-arrays/14673/7 "2019-10-13T23:44:51Z")

</div>

This is exactly what is needed!
