?
Solved

how to store details from file in an object

Posted on 2006-10-19
11
Medium Priority
?
211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

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 1400 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 1400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

741 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