We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › append with multidimensional array
Page Index Toggle Pages: 1
append with multidimensional array (Read 1881 times)
append with multidimensional array
Feb 15th, 2008, 5:39pm
 
Hi Guys,

i am constructing a multidimensional array

String [][] nameOfString = new String[0][0];

then i want to append some things

nameOfString = append();

how does this work?


thanks a lot?
Re: append with multidimensional array
Reply #1 - Feb 15th, 2008, 6:10pm
 
Is there a reason you need a 2D array.  What might be more efficient here is doing a 1D array of ArrayLists.

http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html

Code:

ArrayList[] stuff = new ArrayList[10];
for (int i = 0; i < stuff.length; i++) {
stuff[i] = new ArrayList();
}


then you can always just say:

Code:

stuff[5].add("some string");


etc.
Re: append with multidimensional array
Reply #2 - Feb 15th, 2008, 9:20pm
 
assuming your array has 2 'columns', here is the syntax :

nameOfString = (String[][])append(nameOfString, new String[]{"value of col #1", "value of col #2"});
Re: append with multidimensional array
Reply #3 - Mar 3rd, 2008, 7:25pm
 
thanks antiplastik, thats it.
but it does not work with 3d array?
Re: append with multidimensional array
Reply #4 - Mar 3rd, 2008, 8:02pm
 
having it like this:
nameOfString = (String[][][])append(nameOfString, new String[]{"value of col #1", "value of col #2","value of col #3"});

i get this:
java.lang.IllegalArgumentException: array element type mismatch

Edit:
Where can i finde a reference about the use of Array 2D/3D with focus on append?

How is it possible to only append data to the second column?
Re: append with multidimensional array
Reply #5 - Mar 4th, 2008, 7:38pm
 
no, append and the rest of the array functions only work with a single dimension. otherwise the code for those functions gets really awful really quickly. (and how many dimensions should be supported? 2? 3? 4?)
Re: append with multidimensional array
Reply #6 - Sep 6th, 2008, 10:29pm
 
I am trying to make my own Array of strings from a relational database and a TSV I produced from it. I figured out how to read from it using the example in VIsualizing Data but I tihnk I really need to construct a multiple dimensional array OR according to Daniel's suggestion, an ArrayList. Each line in the tsv file has about 15 entries and there are around 70 entities(in the first dimension of the array eg names on a list)...I just looked into ArrayList and I see two problems - first it sounds like it can only hold 10 lists...and I read in the Processing book that it is a slow way to go...

what's the best way to deal with this sort of data? I want to feed this array or list of arrays to a function which might construct nodes in a graph and treat each one to different stylizations based on the entries/attributes in the array...
Re: append with multidimensional array
Reply #7 - Sep 7th, 2008, 9:46am
 
From your previous post, I believe you need to read the database (TSV file actually) once and deal with the data there, without expanding it.
If that's right, all you need is to use loadStrings() to get an array of lines, then parse them into a double array of strings, since you know how many columns you have: you can declare something like String[][] data = new String[lineNb][COLUMN_NB]; for example.

Wait, looking again, I see that's what Table.pde does, more or less (it has to guess the number of columns).
Why/how this class doesn't fit your needs?
Re: append with multidimensional array
Reply #8 - Sep 7th, 2008, 10:28am
 
I'm just having trouble understanding how to manage my array..

in my first exercise I looped through my rows, assigned entries in specific columns to variables, and then fed them as arguments to my build graph function.
Now I am trying to figure out how to pass the entirety of each row to the constructor as opposed to just one column which was being used to ID each entity. Yes, my example is working but all it does is show the String over an ellipse - what I want is to be able to size the node based on float values associated with each count, colours associated with certain other attributes...
Re: append with multidimensional array
Reply #9 - Sep 7th, 2008, 12:15pm
 
You have two ways to do this, at least.
- Just pass the row (an array) as parameter;
- Create a class to encapsulate the data: each line of data will become an object, already parsed, and you can access easily the fields.
Or just keep the data global... Bad, but OK for a simple sketch.
Re: append with multidimensional array
Reply #10 - Sep 26th, 2008, 11:10am
 
Hi there,
Sorry I am coming late to this discussion. Fry are you saying that with the Table.pde from your Visualizing Data book there is no way to append a new row or manually expand the array?

help would be much appreciated!

Christian
Re: append with multidimensional array
Reply #11 - Sep 26th, 2008, 2:25pm
 
softhook wrote on Sep 26th, 2008, 11:10am:
Sorry I am coming late to this discussion. Fry are you saying that with the Table.pde from your Visualizing Data book there is no way to append a new row or manually expand the array

The Table.pde class on the site doesn't do it, but that's only for simplicity's sake (it's not needed in the example), and it's easy to add methods that will expand the arrays. Here's another version that should take care of the expanding/contracting automatically:

http://benfry.com/writing/parse/Table.java

As of now it's not terribly efficient (it only expands when necessary, even if you're about to add 20 new rows, for instance), but it could be modified to make it more efficient.

(Note that the syntax is a little different since it's made to be in a tab named Table.java.)
Re: append with multidimensional array
Reply #12 - Sep 29th, 2008, 1:49pm
 
Thanks Ben,

Thats great. Unfortunately I don't understand how to implement it in Processing. Is there any chance you could post a working example on how to use the code?

Thanks so much
Christian
Page Index Toggle Pages: 1