Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding Object into an array

 
Kassi Hill
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ola.

Shouldn't that suffice?
Nimo.
 
Kassi Hill
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error message is created by the compiler?
Often, it's very useful in pinpointing compiler errors.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic