Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Search ArrayList

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scroll really far to the right.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 4804
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic