Int array and Int dict

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.

1 Like

Hi, and welcome!

I don’t know if you have seen the reference for the Processing language? It also has intDict.
(And here’s a page with 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.

1 Like

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…

I did take a stab at it :slight_smile:

Using an ArrayList 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.

/* 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;
  }
}
1 Like
// 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();
2 Likes

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

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

# 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: :flushed:

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

# 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()
2 Likes

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

1 Like