File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Search ArrayList

 
John Attard
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

10x
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scroll really far to the right.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Wouter
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 4660
5
Linux Mac OS X VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic