简体   繁体   中英

How do I copy a set of one-dimensional arrays to a two-dimensional array in Processing?

I have these arrays of Strings and I want to load them into an Array of Arrays to process them afterwards.

Specifically, here is my code:

leerHTMLs(String[] urls) {

 String[][] htmlsArr;

    for (int i = 0; i < urls.length; i++) {

      String [i][] htmlsArr = loadStrings(urls[i]);

    }  

}

I get an error, because "String htmlsArr[i][] = loadStrings(urls[i]);" is claearly not a proper way to initialize an array, right? But how can I move these one-dimensional arrays to my multidimensional array?

I've already tried other things except initializing the multidimensional array with a too-big-to-fail number, but that seems like cheating.

well, you said it yourself! It's an array of arrays! That means when you do

htmlsArr[i]

you will get back the element i from this array of arrays, which is of course, an array! When you do

htmlsArr[i][j]

what you get back is the element j in the array which is the element i in the array of arrays called htmlsArr! The final code should look like this:

void leerHTMLs(String[] urls) {
  String[][] htmlsArr;
  htmlsArr = new String[urls.length][0];
  for (int i = 0; i < urls.length; i++) {
    htmlsArr[i]  = loadStrings(urls[i]);
  }
}

So since loadStrings() returns an array of Strings you tell processing to take the output and put it in the element i of array htmlsArr (which is of course an array).

Technically speaking this is not a proper way to do this sort of thing because saying

htmlsArr = new String[urls.length][0];

means make an array with urls.length number of elements and they should all be arrays with 0 number of elements. But then you overwrite the arrays with other arrays with arrays of arbitrary number of elements, specifically as many as loadStrings may give you.

EDIT:

As per Shane's request in the comments, a more proper way to do it is using list in one of the arrays(external or internal one). That is, of course, in my own humble opinion, because since Processing(Java) allows you to do that you might as well consider it proper. The problem is that when you say something in the likes of:

String [][] arr = new String[3][4];

what you mean is essentially a rectangular array sort of like this:

{{  "a","b","c","e"  },
 {  "p","y","f","n"  },
 {  "g","q","r","m"  }}

that is, an array with three arrays in, each containing four elements. It is merely for reasons of intuition that I consider this an "improper" way. If your array was like this:

{{  "a","b","c","e","k","h","s"  },
 {  "p","y"  },
 {  "g","q","r","m"  }}

there is no way to intuitively define it in a new String[3][something] way, although you can always use the stretch in my original answer. (by the way it seems you can also define it with an empty parameter like this: new String[3][] )

But since you are going to leave the second dimension of your array undefined, you might as well turn to lists which are more fit for the "unknown number of elements" job. Since it seems you can't create an array of lists you will have to create a list of arrays like this:

ArrayList<String []> a = new ArrayList<String []>();

As you can see there is no definition of numbers of elements here. ArrayLists can take as many as you can add to them, and you can arbitrarily add and remove elements. So what the above code says is that you have a list of String arrays which are not yet defined. Then, in the for loop that reads your urls you can just go:

a.add(loadStrings(urls[i]));

and now there is a new String array element in your ArrayList!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM