wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Adding Object into an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding Object into an array" Watch "Adding Object into an array" New topic
Author

Adding Object into an array

Kassi Hill
Greenhorn

Joined: Sep 29, 2003
Posts: 24
I have a collection class that creates an array of objects in the constructor. I have a class that contains set and get methods to get the information out of the array created. I have another method that searches through the array for matches to what the user types in. If a match is found, then a boolean value is set to true and the matching inforation is printed out. What I need to do is put the matches found into another array and then loop through the foundMatches array. Below is the code that sets the boolean value, I cannot figure out how to then put the matches into another array.
for(int i=0;i<hotels.getSize();i++){
//verify hotels one at a time
Hotel oneHotel=hotels.getHotel(i);

if(oneHotel.getCity().equals(city)&&oneHotel.getState().equals(state)){
//if at least one hotel is found
found=true;
//print the information here.
This does work and I am able to get the information however, I need to be able to put it into a foundMatch arry to loop throuh to print out. Thank you for your help.
C. Nimo
Ranch Hand

Joined: Mar 23, 2004
Posts: 82
ola.

Shouldn't that suffice?
Nimo.
Kassi Hill
Greenhorn

Joined: Sep 29, 2003
Posts: 24
I added the foundMatch(oneHotel); and that line compiled. Now when I try to loop through this array using:
for(int j=0; j<foundMatch.length; j++){
out.printlin(foundHotel(j));
}
I cannot get the loop to compile, can you see a problem with it??
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
What error message is created by the compiler?
Often, it's very useful in pinpointing compiler errors.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Note that if I wanted to display a line of text to the system console, I'd likely use the command
System.out.println("some string");
Also, note the spelling of the println method.
nikos sokaf
Ranch Hand

Joined: May 01, 2004
Posts: 57
Look at the java.util.*; package.
Import java.util.*;

and then :
ArrayList a=new ArrayList();
a.add("Some String") or a.add(index,"Some String");
If you wanna handle the values:
Object o[]=a.toArray();
for(int i=0;i<o.length;i++)
System.out.println(o[i].toString());
or find another way to handle the values...It is easy
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Adding Object into an array