Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to store details from file in an object

Posted on 2006-10-19
11
Medium Priority
?
213 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

618 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