wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes array questions and classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array questions and classes" Watch "array questions and classes" New topic
Author

array questions and classes

Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
I'm trying to build a hotel booking app. I can code but not in Java (or anything else OO). I have a few questions :

1) If I create an array is it possible to just keep adding to it or do I always have to have a max number of records?
String Booking[] = new String[NUM_DAYS];

2) Would I keep track of what record I'm up to with a booking_cnt variable or is there a better way?

3) If booking was a class with variables that pointed to a customer class etc how would I point the booking instance at the customer instance.

4) Presume I just then move any functions (methods) that work on a booking into the booking class etc.

5) Presume "this" will be available whenever I create a new class - can I then use the "this" to fire off mouse trigger events or does this have to be in a frame etc? ie with
addMouseListener(this);


Arcade : Alien Swarm
Board : Chess - Checkers - Connect 4 - Othello
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
1) If I create an array is it possible to just keep adding to it or do I always have to have a max number of records?
String Booking[] = new String[NUM_DAYS];


The size of an array in Java is fixed at the time of declaration. You'd be better off using java.util.ArrayList, it offers the level of abstraction that you seem to be looking for.

2) Would I keep track of what record I'm up to with a booking_cnt variable or is there a better way?


See 1

3) If booking was a class with variables that pointed to a customer class etc how would I point the booking instance at the customer instance.

You would use the Booking constructor or a setter method.



4) Presume I just then move any functions (methods) that work on a booking into the booking class etc.
There's not really a question there, but that's a good idea.

5) Presume "this" will be available whenever I create a new class - can I then use the "this" to fire off mouse trigger events or does this have to be in a frame etc? ie with
addMouseListener(this);
I really am not sure what you mean by this. The concept of a mouseListener is tightly coupled to the concept of some type of GUI.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18500
    
  40

1) If I create an array is it possible to just keep adding to it or do I always have to have a max number of records?
String Booking[] = new String[NUM_DAYS];


Arrays in Java are fixed size, so you can't "keep adding". Take a look at the collections classes -- particularly classes that implement java.util.List for object types that you can "keep adding" to.

[EDIT: just started answering the first question, and already beaten to the answers... ]
Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
Thanks for your help guys - it's kind of fun learning about new concepts and then trying to immediately code them.
 
jQuery in Action, 2nd edition
 
subject: array questions and classes
 
Similar Threads
URLyBird - Data file spec
Newbie validation with textfile
array initialization within class
I Need to Write a Method Which Adds a Class Once Run
constructor with array type.