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">
<item>
<title>book1 title</title>
<chapters>17</chapters>
<pages>450</pages>
</item>
<item>
<title>book2 title</title>
<chapters>12</chapters>
<pages>326</pages>
</item>
</array>



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
myBOOKS[1][pages]=450
jjanes
Freshman
Freshman
 
Posts: 7
Joined: Sat Feb 04, 2012 5:51 am

Top

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">
<item>Title1,12,350</item>
<item>Title2,10,250</item>
etc.


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];
    i++;
    }//while


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

Hope this helps.

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

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 21 guests