0

I want to store three values in a 2D type in java. I know that we can use List and ArrayList for storing 1D values but I need to store more than one field in a specific record. For example i have to enter the details for multiple columns i.e. (1,1),(1,2),(1,3) for details such aaaa, bbbb, cccc for a person and store them in one single row(which may consist of values which are other than string type). It should run in a loop and once details of a person is stored, it should store (2,1),(2,2),(2,3) i.e. again for a new person. How to do that?

And later on, how to retrieve and send the complete set to database together? Please help..

5
  • I would rather use an in memory database, create a table for a person and insert the data in the person table... Commented Sep 26, 2014 at 14:39
  • ya but i am using oracle 10g and using jdbc to connect it to java. i am actually developing a swing application through which i need to collect data and send to database and there are multiple fields for a single person. so i need to know how to store ? Commented Sep 26, 2014 at 14:45
  • 1
    Why not make Person a class and have it contain fields or lists for all required details for a person. Then you can create a list or whatever of Persons. Commented Sep 26, 2014 at 14:45
  • @BorisPavlović How to create an in memory database? Commented Sep 26, 2014 at 14:54
  • @HannoBinder ya ok..will try that.. Commented Sep 26, 2014 at 14:54

1 Answer 1

3

What you might want to do is to create a class that holds all of the information you want to keep related to a single record if it represents a concrete thing and use the List and ArrayList to store those.

What I mean by concrete thing is something that has a finite set of information that will stay the same over each object.

Something like:

public class Person
{
    String name;
    Integer age;
    // etc...
}

This gives you two advantages over using something like a 2D array. First, it will make reading your code easier, since instead of having to remember that arrayName[x][0] is whatever you decide the first field is, you can access it using something like listItem.attributeName. The second advantage is that you can abstract out any common datahandling tasks as class methods instead of having to bloat your main class with it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.