Solved

how to store details from file in an object

Posted on 2006-10-19
11
206 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
ID: 17766815
ArrayList<your object>.

;JOOP!
0
 

Author Comment

by:prograMNewbie
ID: 17766830

An ArrayList of objects?
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17766853
Yep!
Allows you to initially ignore the size of the file.

;JOOP!
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:prograMNewbie
ID: 17766887


>>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
ID: 17766925
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
 
LVL 24

Expert Comment

by:sciuriware
ID: 17766937
Typo:  Coversions -> Conversions

;JOOP!
0
 
LVL 9

Expert Comment

by:shinobun
ID: 17767252
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
ID: 17767367
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
ID: 17768698
>> 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
ID: 17809958

thanks for all the comments.

;)
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17810045
:)
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

813 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

17 Experts available now in Live!

Get 1:1 Help Now