Solved

how to store details from file in an object

Posted on 2006-10-19
11
209 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 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

695 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