Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array of an Arraylist? 1

Status
Not open for further replies.

k4ghg

Technical User
Joined
Dec 25, 2001
Messages
192
Location
US
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

 
How about:
Code:
import java.util.*;
public class Test {

    public static void main(String[] args) {

        ArrayList layerDepth = new ArrayList();

        // sample 0:
        // depth: 10, 30, 12, 4
        ArrayList depth = new ArrayList();
        depth.add(new Integer(10));
        depth.add(new Integer(30));
        depth.add(new Integer(12));
        depth.add(new Integer(4));
        layerDepth.add(depth);

        // sample 1:
        // depth: 5, 6
        depth = new ArrayList();
        depth.add(new Integer(5));
        depth.add(new Integer(6));
        layerDepth.add(depth);

        // print out
        for(int sample = 0; sample < layerDepth.size(); sample++) {
            System.out.println("sample "+sample+": ");
            System.out.print("depth: ");
            depth = (ArrayList) layerDepth.get(sample);
            for(int i = 0; i < depth.size(); i++) {
                System.out.print(depth.get(i)+
                    (i != depth.size() - 1 ? ", " : ""));
            }
            System.out.println();
        }
    }
}
Cheers, Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top