Repeat through ArrayLists

I have two questions. I have an object that is of type ArrayList , and for this case, call it "Car" .

I made 2 of them:

 Car car1 = new Car(); Car car2 = new Car(); 

I have a function to add elements to Car objects:

 car1.addPart("Front Wheels"); car1.addPart("Rear Wheels"); car1.addPart("Rear View Mirror"); car2.addPart("Rims"); car2.addPart("Steering Wheel"); car2.addPart("Bumper"); 

I need to have a function called sameContents() that I can call car1 :

 car1.sameContents(car2); 

which runs in an object of type ArrayList and checks it with car1 to see if they have the same content and in the same order.

 public boolean sameContents(Car c) { ArrayList<String> other_car = c; // error: Type mismatch: // cannot convert from Car to ArrayList<String> for (String c : this.parts) { System.out.println(c); for(String oc : other_car) { // stuff } } } 

It seems that I have all kinds of problems associated with this. I cannot get the other_car variable to be used in the foreach loop.


The second thing to do is transferContents .

It is called like:

 car1.transferContents(car2); 

which transfers elements to car2 in car1 , and then leaves car2 empty. I can't seem to get ArrayList to work again in the foreach loop, which I think I need.

  public void transfer(Car c) { // code for transfer method. // this.parts is the arraylist of car parts for (Car c: c) { this.parts.add(c); } // not sure how to set car2 to empty... } 
+7
source share
3 answers

For some List<T> foo , for foreach loops, for example:

 for(T item : foo){ // body } 

is just the shorthand syntax for this idiom:

 Iterator<T> iter = foo.iterator(); while(iter.hasNext()){ T item = iter.next(); // body } 

To check that there are more elements in the list, you call iter.hasNext() to get the next element, you call iter.next() .

Walking through the two lists can be done by storing about 2 iterators, checking that both iterators have more elements, and then extract those elements. We can eliminate some boundary conditions in different length lists by realizing that different length lists cannot contain the same elements (since one list has more than the other).

From your description, it looks like Car contains a List<String> parts; property List<String> parts; Therefore, we can formulate a solution as:

 // different sizes, can't be equal if(this.parts.size() != other.parts.size()){ return false; } // get iterators Iterator<String> left = this.parts.iterator(); Iterator<String> right = other.parts.iterator(); // safe to only check `left` because the lists are the same size while(left.hasNext()){ // check if left part is equal to the right part if(!left.next().equals(right.next())){ // values are different, know at this // point they're not equal return false; } } // at this point, have exactly the same values // in the same order. return true; 

As for your transferContents method, you have the right idea, but you cannot iterate over Car , you need to iterate over List<String> parts . To remove individual parts, you can use the remove() method, called as the add method, or remove all elements, you can call clear()

Combining this:

 for (String part : c.parts) { this.parts.add(part); } c.parts.clear(); 
+22
source

You can rely on java api to do whatever you need. ArrayList is equal to the check method when comparing two lists. You can use the removeAll () and addAll () methods to transfer content.

 public class Car { private final List<String> parts = new ArrayList<String>(); public void addPart(String p) { parts.add(p); } public boolean sameContents(Car c) { return this.parts.equals(c.parts); } public void transfer(Car c) { final List<String> temp = new ArrayList<String>(c.parts); temp.removeAll(this.parts); this.parts.addAll(temp); c.parts.clear(); } } 
0
source

Your car should not be a list of arrays, but it does . For example. something like that:

 class Car { ArrayList<String> parts; // ... } 

Then your sameContents method can simply call the .equals() list to compare:

 public boolean sameParts(Car other) { return this.parts.equals(other.parts); } 

Similarly, to transfer parts from another car, use the list methods in the add part of your list, and then clear another list.

0
source

All Articles