I have the following class as a example to a similar problem im having ...

Class Car


public class Car {
        private String name;
        public Car(String name) {
            this.name = name;
        public String getName() {
            return name;

Class CreateCarFromList


public class CreateCarFromList {
        private List carNames = new ArrayList();
        private List carObject = new ArrayList();
        /** Creates a new instance of CreateCarFromList */
        public CreateCarFromList() {
           Iterator it = carNames.iterator();
           while (it.hasNext()) {
               Car x = new Car((String) it.next());

in the last class i have an arraylist,carNames, with the following value
["Red", "Blue", "Yellow"]

than i do an iteration and create an instance of an object in each iteration and add it to another arraylist, carObject

my problem is what do i need to do, to add to the Car class or perhaps to CreateCarFromList Class.

so that i can say for a

String s = "Red";

give me from carObject list the Car object that has that attribute. (hope this make sense)



10 Years
Discussion Span
Last Post by fdrage

Nope, I don't get you.

Are you trying to create an array of objects.

E.g you have an array of Car objects.

Where the object Car can have the attributes, say Colour, and model:

class Car
   String Colour;
   String Make;

Once you work with this kind of basic structure, everything else should fall into place right? No matter what you are doing.

For example here's an example:

class Car
  private String colour;
  private String make;


  public Car(String c, String m)
   // constructor
   colour = c;
   make = m;


  public void displayCar()
  System.out.print(" Colour: " + colour);
  System.out.print(", Make: " + make);

  public String getColour() // get last name
    return colour; 

  public String getMake()
    return make;
} // end class Car

class ArrayInOb
   private Car[] a; // ref to array a
   private int nElems; // number of data items


   public ArrayInOb(int max) // constructor
    a = new Car[max]; // create the array
    nElems = 0; // no items yet

  // put car into array
  public void insert(String last, String first)
  a[nElems] = new Car(last, first);
  nElems++; // increment size

  public void display() // displays array contents
  for(int j=0; j<nElems; j++) // for each element,
   a[j].displayCar(); // display it

public void insertionSort()
int in, out;
for(out=1; out<nElems; out++) // out is dividing line
  Car temp = a[out]; // remove marked car
  in = out; // start shifting at out
  while(in>0 && // until smaller one found,

  a[in] = a[in-1]; // shift item to the right
   --in; // go left one position
  a[in] = temp; // insert marked item
  } // end for
} // end insertionSort()

} // end class ArrayInOb
class CarList
  public static void main(String[] args)
   int maxSize = 100; // array size
   ArrayInOb arr; // reference to array
   arr = new ArrayInOb(maxSize); // create the array

   //assign values here, the example is hard
   //coded but you can just extract them from an array
   //or text file

   arr.insert("Blue", "Ford");
   arr.insert("Red", "Toyota");
   arr.insert("Aqua", "Limo");
   arr.insert("Black", "Jaguar");
   arr.insert("White", "BMW");

   System.out.println("Before sorting:");
   arr.display(); // display items
   arr.insertionSort(); // insertion-sort them by make
   System.out.println("After sorting by make:");
   arr.display(); // display them again
   } // end main()
} // end class ObjectSortApp

it was quite simple actually, with the use of HashMap instead of an ArrayList to put the car object in (key: "car colour", value: car object)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.