Solved

how to store details from file in an object

Posted on 2006-10-19
11
205 Views
Last Modified: 2010-03-31
Hi all,

I have data in a file that I want to store in some kind of object. This file contains lines of data (name, age, gender) like so:

angela 30 female
mike 45 male
danny 20 male
sharon 27 female

What is the best way to store this data, in my view there are two possible options:

1. A list of objects where one line corresponds to one object.

2. One object where which keeps seperate Lists for each field in the file.

What are your thoughts?

Thanks

0
Comment
Question by:prograMNewbie
  • 6
  • 3
  • 2
11 Comments
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
ArrayList<your object>.

;JOOP!
0
 

Author Comment

by:prograMNewbie
Comment Utility

An ArrayList of objects?
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
Yep!
Allows you to initially ignore the size of the file.

;JOOP!
0
 

Author Comment

by:prograMNewbie
Comment Utility


>>Allows you to initially ignore the size of the file

Could you elaborate please?
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 350 total points
Comment Utility
Assume your class is named Xyz{}

ArrayList<Xyz> data = new ArrayList<Xyz>(initial number of objects reserved);

// Open the file, say to 'input'.

String s;

     while((s = input.readLine()) != null)
     {
           data.add(new Xyz(s));  // Coversions in the constructor of Xyz
     }

// Who cares how many lines the file has.

;JOOP!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
Typo:  Coversions -> Conversions

;JOOP!
0
 
LVL 9

Expert Comment

by:shinobun
Comment Utility
You should consider keeping the type an interface (List<...> [1]).  That way, you can change the implementation later easily.

ArrayList [2] is only one way you can do it, which is backed by an Object array.  You also have choices like LinkedList [3], which uses the good old "link structure" as the back end.  ArrayList is better at random access, and LinkedList is more efficient and constant in size growth.  You might want to look at the javadoc for all implementations and pick the one that suits your needs.

[1] http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html
[2] http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html
[3] http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedList.html
0
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 350 total points
Comment Utility
Building a list from a file is an operation in one batch.
In that case ArrayList is very efficient.
LinkedList only wins when additions grow over a long time.

Introducing interfaces and the sort comes later.
;JOOP!
0
 
LVL 9

Expert Comment

by:shinobun
Comment Utility
>> LinkedList only wins when additions grow over a long time.

Also if you remove and insert elements over time.  :)

>> Introducing interfaces and the sort comes later.

Sorry, but I can't agree on that.  People writing java without knowledge on basic concepts of OO like polymorphism gets more dirty code out in the world.  You can say, "this is the idiom for creating new lists":

List<Xyz> data = new ArrayList<Xyz>(initial number of objects reserved);

And have newcomers believe that, and we have one more clean code.  This raises the industry average, and also helps you look after the code these people write.

You might argue that that will risk job security, but you just have to keep ahead in technology.

Anyways, just my opinion.  YMMV.  :)
0
 

Author Comment

by:prograMNewbie
Comment Utility

thanks for all the comments.

;)
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
:)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now