We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm trying to basically get this double loop in order to access the values of the data structure below: p1[0], p1[1], p1[2], p2[0], p2[1], etc. Not really sure why this isn't working
for (int i = 1; i < 3; i++) {
for (int j = 0; j < 3; j++){
println("p"+i[j]);
}
}
//dummy stanza one
String[] p1 = {
"this is the first",
"this is the second",
"this it the third"
};
//dummy stanza two
String[] p2 = {
"1",
"2",
"3"
};
//dummy stanza three
String[] p3 = {
"one",
"two",
"three"
};
Answers
You can't create variables from strings like that. You could use a
HashMap
instead. More info can be found in the reference.How would you use this to solve the problem? I'm not sure I understand? I need to programmatically reference this data structure in a for loop...
If you have a lot of characters, you store them in a string.
If you have a lot of strings, you store them in an array of strings.
If you have a lot of arrays of strings, you... store them in different arrays of strings? NO!
If you have a lot of arrays of strings, you store them in an ARRAY of arrays of strings.
The problem @Tfguy44 is that the data is mixed type.
I have 1.) a string and 2.) two floats, a lat and a lon
but I don't think processing allows you to do mixed type arrays...
Shame on me for not including my actual data structure; here it is:
So, what I'm really trying to pull is:
and so forth and so on, but with a for loop
You need a class to put all that data into, keep it all together in an object rather than combining it by type in disjointed arrays. There's a FAQ over in Common Questions that has examples.
@koogs; I'm not exactly sure that's the strategy I want to take, but could you link to that FAQ? Right now, I have a "reader class" that pulls in this array data does a pretty complex/cool visualization (embedding these strings in a 3D map). I basically just want to have an easy way to write down:
string/lat/lon string/lat/lon etc.
and then this other class will hoover it up and make the data visualization.
It's exactly what classes are for.
https://forum.processing.org/two/discussion/8081/from-several-arrays-to-classes
https://Processing.org/reference/HashMap.html
https://Docs.Oracle.com/javase/8/docs/api/java/awt/geom/Point2D.Float.html
Another flavor! This time switching to a
class
w/ 1 String + 1 Point2D.Float fields.All contained by a 2D array: :-bd
"Labeled Coords 2DArray (v2.0)" is almost the same as previous (v1.0).
But LabelCoord rather than having 1 Point2D.Float field called coord, it instead
extends
Point2D.Float.This way,
class
LabelCoord has direct access to Point2D.Float's fields x & y and its methods. \m/@GoToLoop; I'm very intrigued by the data structure above; I'm going to try it, I very much think it is going to work. Thank you so much!
@GoToLoop also, question can I have stanzas with a different amount of entries? e.g. stanza 0 has 3 entries, stanza 1 has 2 entries?
It's your code. You can do whatever you want.
But it sounds like you're looking for the
append()
function or theArrayList
class. The reference is your best friend.@KevinWorkman; thank you for responding; I'm just looking to take the above example and be able to have dummy stanzas defined with a different amount of entries. I want to define it all in advance. I added another LabelCoord Object to "dummy stanza three", but I'm not sure how to loop through the double-for loop below without getting snagged on a null.
@KevinWorkman; are you suggesting that I change this array into an arraylist in order to be able to achieve that?
Sure! An array's last dimension can be created empty
[]
w/o specifying its length: :-bdfinal LabelCoord[][] stanzas = new LabelCoord[STANZAS][COORDS];
final LabelCoord[][] stanzas = new LabelCoord[STANZAS][];
However, now we're gonna have to create the last dimension as many times as the 1st dimension's length. #:-S
For "Labeled Coords 2DArray (v2.0)", let's say "dummy stanza two:" got now only 2 entries instead.
Previous version w/ always length = 3 for
new LabelCoord[STANZAS][COORDS];
:Now w/ just length = 2 for
new LabelCoord[STANZAS][];
:Alternatively, you can instantiate the inner dimension w/ the desired length alone; and then assign its entries separately: ;)
@GoToLoop ; 1.) ultra insane thanks, 2.) check out what the code actually does: https://www.instagram.com/p/BGnt9WFiuEB/?taken-by=saito_koriel (3D map of city embedded text, virtual cameras head from point to point; creates a visualization based on this data we're talking about here), 3.) OK, got it, don't specify the length of the array in advance, but then why do you need this line:
and why doesn't that declaration mess up the code? Everything seems to be working and doing exactly what I need it to do. Pasting it below just so that you can see that it checks out:
import java.awt.geom.Point2D;
Well, it's not messing up b/c COORDS isn't being used now. Just the constant STANZAS. :P
You can simply erase constant COORDS outta the code since it isn't needed anymore. :-h
It's merely a formality to state clearly an array's dimension for the next guy studying the code. ;))
In order to iterate over the 2D array, you can use 2 "enhanced"
for ( : )
loops in place of "regular"for ( ; ; )
1s: *-:)https://Processing.org/reference/for.html
And given the last dimension is an array, we can go w/ just 1
for ( : )
loop + printArray(): :ar!https://Processing.org/reference/printArray_.html
Oops! Just realized you're basing your code on v1.0 rather than v2.0! b-(
B/c under v1.0,
class
LabelCoord doesn'textends
Point2D.Float, we can't use the latter as the datatype for the former! :-@For those enhanced loops, just replace the more generic Point2D w/ the more specific LabelCoord datatype: :)>-
Notice though this solution now also works under v2.0, given the class is both datatype LabelCoord & Point2D.Float. :\">