• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array to Arraylist  RSS feed

 
Ronnie Coh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using and Arraylist for sor soil samples because I don't know the number of samples and the depth of each sample. I am having a problem setting up my Arraylist and would appreciate some help.

In general the sample can be handled with a two diminsional array.

int[][] layerdepth = new int[][]; // where layerdepth[sample number
unknow][depth unknow]

int[][] watercapity = new int[][]; // where water capacity[sample number
unknow][depth unknow]

I thought I could add this to an Arraylist By:

ArrayList asoildata = new Arraylist;
asoildata.add(sample number, layerdepth);

But it will not work. I need to retrieve data by sample number, and suggestions would be appreciated.

Thanks...Ronnie
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ronnie,

I have read your post several times, but I'm still not sure what you are trying to accomplish.
The last sentence
I need to retrieve data by sample number

seems to suggest that you have to take a look at a Map instead of an ArrayList

Bart.
 
Joe Ess
Bartender
Posts: 9443
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Works for me. Is your depth a primitive? It has to be an Object. Try an instance of java.lang.Integer. I don't get why you are using a 2d array. I'd think each sample would correspond to one depth, which can be done in a 1d array of depths, with the sample number as the index into the array.
 
Ronnie Coh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joe Ess:
Works for me. Is your depth a primitive? It has to be an Object. Try an instance of java.lang.Integer. I don't get why you are using a 2d array. I'd think each sample would correspond to one depth, which can be done in a 1d array of depths, with the sample number as the index into the array.


I tried a multi - dimentional array. My problem is that I do not know the number of depths, they vary by sample.

I tried the ArrayList and was hoping to get a "multi deminsional" option but could not get it to work.

Thanks...
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays don't need to be square.
You can also use a hybrid solution where you add arrays of different sizes (or ArrayLists) to an array or ArrayList.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More specifically, Java does not support the concept of a multi-dimensional array, which is distinct from an (array [of arrays]*) that is does support.

A language such as COBOL, supports true multi-dimensional arrays. The common misconception that Java supports multi-dimensional arrays is the cause of countless problems (at least in my experience).

An array can hold elements of arbitrary type, which in turn, may be an array.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!