# Int array and Int dict

**URL:** <https://discourse.processing.org/t/int-array-and-int-dict/23879>\
**Category:** Beginners\
**Created:** [September 15, 2020, 8:03pm UTC](https://discourse.processing.org/t/int-array-and-int-dict/23879 "2020-09-15T20:03:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rattus](https://avatars.discourse-cdn.com/v4/letter/r/3e96dc/32.png) [@rattus](https://discourse.processing.org/u/rattus)\
**Post date:** [September 15, 2020, 8:03pm UTC](https://discourse.processing.org/t/int-array-and-int-dict/23879/1 "2020-09-15T20:03:12Z")

</div>

hi, i am foreign with java, and processing but i know just enough to “get around”  
now, i have a lattice/2d array, and I would like to assign a dictionary to each one  
ex:

> int myArray = new int[2][2];  
> for (int x = 0; x \< 2; x++) {  
> for (int y = 0; y \< 2 y++){  
> myArray[y] = new IntDict();  
> }  
> }

i would then like to be able to access each one via,

> myArray[1][2].get(“prev\_hue”,255)

so simply put it, i would like to store more then one value in each cell, or locatio in the array.

---

<div class="post-metadata">

**Author:** ![raron](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/raron/32/13651_2.png) [@raron](https://discourse.processing.org/u/raron)\
**Post date:** [September 15, 2020, 11:06pm UTC](https://discourse.processing.org/t/int-array-and-int-dict/23879/2 "2020-09-15T23:06:35Z")

</div>

Hi, and welcome!

I don’t know if you have seen the [reference](https://processing.org/reference/) for the Processing language? It also has [intDict](https://processing.org/reference/IntDict.html).  
(And here’s a page with [tutorials](https://processing.org/tutorials/), just in case).

Testing this a bit for myself just now, it might not be as straight-forward as you hope. I suspect you have to put the IntDict in a class, which you then can make an object array out of (I might be wrong, not a Java / Processing expert either).

Hope it’s of some help.

---

<div class="post-metadata">

**Author:** ![rattus](https://avatars.discourse-cdn.com/v4/letter/r/3e96dc/32.png) [@rattus](https://discourse.processing.org/u/rattus)\
**Post date:** [September 16, 2020, 1:19am UTC](https://discourse.processing.org/t/int-array-and-int-dict/23879/3 "2020-09-16T01:19:26Z")

</div>

that was helpful, how ever would you mind trying to reword it for someone who has been working with python for most of thier life? I guess i was spoiled when it comes to data structures…

---

<div class="post-metadata">

**Author:** ![raron](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/raron/32/13651_2.png) [@raron](https://discourse.processing.org/u/raron)\
**Post date:** [September 16, 2020, 1:56am UTC](https://discourse.processing.org/t/int-array-and-int-dict/23879/4 "2020-09-16T01:56:15Z")

</div>

I did take a stab at it 🙂

Using an [ArrayList](https://processing.org/reference/ArrayList.html) might work as well (though I’m uncertain about a two- or more dimensional ArrayList, so maybe not?).

I just expanded the IntDict example to use a wrapper class. I haven’t implemented all IntDict methods, just a few to demo.

```auto
/* IntDict multidimensional array test
*/

intDictWrapper inventory[][];
String [] items = { "cd", "dvd", "blueray", "tapes", "records" };

void setup() {
  size(200, 200);
  inventory = new intDictWrapper[2][3];
  
  for (int i=0; i<2; i++) {
    for (int j=0; j<3; j++) {
      inventory[i][j] = new intDictWrapper();
      int randomlength = int(random(5)) + 1; // 1-5 items
      for (int item=0; item<randomlength; item++) {
        inventory[i][j].set(items[item], int(random(100)));
      }
    }
  }

  for (int i=0; i<2; i++) {
    for (int j=0; j<3; j++) {
      println("[" + i + "] [" + j + "] " + inventory[i][j].listAll());
    }
  }
  noLoop();
  fill(0);
  textAlign(CENTER);
}

void draw() {
  try {
    int numCDs = inventory[0][1].get("cd"); // getting specific value 
    text(numCDs, width/2, height/2);
  } catch (Exception e) {
    e.printStackTrace();
  }
}

class intDictWrapper
{
  IntDict inventory;
  
  intDictWrapper()
  {
    inventory = new IntDict();
  }
  
  void set(String item, int value)
  {
    inventory.set(item, value);
  }
  
  int get(String item)
  {
    return inventory.get(item);
  }
  
  void remove(String item)
  {
    inventory.remove(item);
  }
  
  IntDict listAll()
  {
    return inventory;
  }
}

```

---

<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:** [September 16, 2020, 3:43am UTC](https://discourse.processing.org/t/int-array-and-int-dict/23879/5 "2020-09-16T03:43:30Z")

</div>

```auto
// https://Discourse.Processing.org/t/int-array-and-int-dict/23879/5
// GoToLoop (2020/Sep/16)

final IntDict[][] matrixDict = new IntDict[2][2];

for (final IntDict[] rowDict : matrixDict)
  for (int len = rowDict.length, i = 0; i < len; ++i)
    rowDict[i] = new IntDict();

final IntDict dict_0_1 = matrixDict[0][1];

dict_0_1.set("prev_hue", 255);
dict_0_1.set("curr_hue", -1);
dict_0_1.print();

final int prev_hue = dict_0_1.get("prev_hue");
println(prev_hue);

final int curr_hue = dict_0_1.get("curr_hue");
println(curr_hue);

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:** [September 16, 2020, 6:09am UTC](https://discourse.processing.org/t/int-array-and-int-dict/23879/6 "2020-09-16T06:09:43Z")

</div>

> [@rattus](#):
>
> … someone who has been working with Python for most of their life?

Processing’s IDE (PDE) got a Python Mode flavor too: 🐍

> **[Python Mode for Processing](https://py.processing.org/)**
>
> Python Mode for Processing extends the Processing Development Environment with the Python programming language.

This is the corresponding port from the Java Mode sketch I’ve posted above to Python Mode: 😉

```auto
# https://Discourse.Processing.org/t/int-array-and-int-dict/23879/6
# GoToLoop (2020/Sep/16)

from processing.data import IntDict

matrixDict = tuple(
    tuple(IntDict() for col in range(2))
    for row in range(2))

dict_0_1 = matrixDict[0][1]

dict_0_1.set("prev_hue", 255)
dict_0_1.set("curr_hue", -1)
dict_0_1.print()

prev_hue = dict_0_1.get("prev_hue")
print prev_hue

curr_hue = dict_0_1.get("curr_hue")
print curr_hue

exit()

```

But if you don’t mind losing IntDict’s insertion-order feature neither its unique methods: 😳

> **[IntDict / Reference](https://processing.org/reference/IntDict.html)**
>
> A simple class to use a String as a lookup for an int value. String "keys" are associated with integer values.

You can just use Python’s builtin dict container instead, which is much simpler: 🤩

> **[Dictionary \\ Language (API)](https://py.processing.org/reference/dictionary.html)**
>
> Python Mode for Processing extends the Processing Development Environment with the Python programming language.

```auto
# https://Discourse.Processing.org/t/int-array-and-int-dict/23879/6
# GoToLoop (2020/Sep/16)

matrixDict = tuple(
    tuple({} for col in range(2))
    for row in range(2))

dict_0_1 = matrixDict[0][1]

dict_0_1["prev_hue"] = 255
dict_0_1["curr_hue"] = -1
print dict_0_1

prev_hue = dict_0_1["prev_hue"]
print prev_hue

curr_hue = dict_0_1["curr_hue"]
print curr_hue

exit()

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [September 19, 2020, 7:53pm UTC](https://discourse.processing.org/t/int-array-and-int-dict/23879/7 "2020-09-19T19:53:54Z")

</div>

…and one more option is #p5py, which is a Processing API implemented directly in Python3.

> **[p5py/p5](https://github.com/p5py/p5/)**
>
> p5 is a Python package based on the core ideas of Processing. - p5py/p5
