File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Search ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Search ArrayList" Watch "Search ArrayList" New topic

Search ArrayList

John Attard

Joined: Jan 16, 2010
Posts: 2
Hi all,

I'm basically coding an Insurance Database in Java using an Arraylist. What works so far is the adding, searching and deleting by index, saving and loading from a binary file. However im finding a great problem in searching through the arraylist by a variable.

However i have 2 classes : PrivateCarPolicy which holds the getters and setters and the toString() method and PrivateCarPolicyTable which holds all the methods for database control.

My code is about 1700 lines long so ill only post the revelant parts.

The toString() Method:

The Arraylist:

The Method to add Policies :

Note that im not including all methods or else this post will never end ;)

The Printing Method that displays the elements in the arraylist

AND LAST BUT NOT LEAST THE FAMOUS METHOD for searching by a variable which is not working :/

John Attard

Joined: Jan 16, 2010
Posts: 2
Sorry for this but i cant seem to find the edit button. Basically if someone can help me out with the searching it would be great dont hesitate to ask for some piece of code that is needed to solve this post.

David Newton

Joined: Sep 29, 2008
Posts: 12617

Scroll really far to the right.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I seriously hope that this is an exercise and not a real project. First I would refactor the class and create the classes Customer, Policy, Car. Read something about cohesion and OOP.
If you want to store a large number of objects in your Arraylist you should be thinking about using a real database because otherwise your performance will basically suck :P.

Why did you choose to use an ArrayList and not a LinkedList?
Variables should begin with a lowercase letter.

Good luck.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
David Newton

Joined: Sep 29, 2008
Posts: 12617

There's no particular reason to use a LinkedList if you don't need bi-directional navigation, and if you need indexed access, I'd assume (without any testing) that an ArrayList would provide better performance.

In any case, whichever is used, the variable should be defined as a List (as opposed to the specific type), unless you need to enforce its ArrayList-ness.
But that's not the issue at this point, I suspect.

ItDoesntWorkIsUseless: you'll need to be more specific as to the problem(s) you're having.
Pat Farrell

Joined: Aug 11, 2007
Posts: 4659

John Attard wrote:My code is about 1700 lines long so ill only post the revelant parts.

You have one class that has 1700 lines of code and does not work? If so, the first issue is to have a smaller amount of code, and get that working.

As others have said, your class is too big, too complex, and too hard to debug.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
Pat nailed it!

A really long class is a symptom of need to redesign. I once worked on maintaining an existing Fortran program having 4000 lines in the main function and 4 tiny subroutines - GAH what a disaster.

There was a saying I liked in Forth - if your method takes more than one screen to display you are doing something wrong.

I agree. Here's the link:
subject: Search ArrayList
It's not a secret anymore!