multi dimensional arrays structure

Put your problem here if it does not fit any of the other categories.

multi dimensional arrays structure

Postby jjanes » Sat Feb 04, 2012 7:09 am

I have some information I would like to load via an xml file

I believe the xml structure would appear like this
<array name="BOOKS">
<title>book1 title</title>
<title>book2 title</title>

I would want to use something like this to load a java array

final String[] myBOOKS = getResources().getStringArray(R.array.BOOKS);

My question is this. What is the syntax used to work with any of the parameters in the array? For instance,
then number of chapters in the second element or pages in the first element
is it as simple as:

myBOOKS[2][chapters]= 12
Posts: 7
Joined: Sat Feb 04, 2012 5:51 am


Re: multi dimensional arrays structure

Postby Phyll » Mon Feb 06, 2012 1:46 am

Hi jjanes,

No I don't think its that easy. Firstly I've never seen any examples of reading in multidimensional arrays with the resource loader. I don't use things like that too much, but I do bring multidimensional arrays in from other places. You could have an array of strings with some sort of delimited strings in each larger string in the array resource. Then get each large array and parse it into its small strings. The other way to do it would be to use SQLite to store and manipulate the data. And I really don't think there's any connection between the names you use in the xml file and the elements of an array they are subsequently loaded into.

I think you could store your books in an array that would have a structure like this:

Code: Select all
<array name="BOOKS">

You could load them into a string array so then you would know how many you have, then create the final multidimensional array and put them into it like this:

Code: Select all
String[] temp = getResources().getStringArray(R.array.BOOKS);
String[][] myBooks = new String[temp.length][3];
int i = 0;
String[] each = null;
while (i < temp.length) {
    each = temp[i].split(",");
    myBooks[i][0] = each[0];
    myBooks[i][1] = each[1];
    myBooks[i][2] = each[2];

A little more code but it will work well and you can always improve it I"m sure.

Hope this helps.

Master Developer
Master Developer
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am


Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 8 guests