Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6581685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:11:06+00:00 2026-05-25T16:11:06+00:00

I have two questions. I have an object here that is of type ArrayList

  • 0

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

I have made 2 of them:

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

I have a function to add items to those 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 on car1:

car1.sameContents(car2);

which passes in an object of type ArrayList and checks it with car1 to see if they have the same contents 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
        }
    }
}

I seem to be having all sorts of issues with this one. I can’t get the other_car variable to be used in a foreach loop.


The second one that needs to be done is transferContents.

It’s called like:

car1.transferContents(car2); 

which transfers the items in car2 into car1 and then leaves car2 empty. I can’t seem to get the ArrayList to work again in a foreach loop which is what 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...
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:11:06+00:00Added an answer on May 25, 2026 at 4:11 pm

    Given some List<T> foo, foreach loops, e.g.:

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

    are just a shorthand syntax for this idiom:

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

    To check that there are more items in the list, you call iter.hasNext(), to retrieve the next item, you call iter.next().

    Walking two lists can be done by keeping around 2 iterators, checking that both iterators have more elements, and then retrieving those elements. We can eliminate some boundary conditions on 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 sounds like Car contains a property List<String> parts;, so 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 the Car, you need to iterate over the List<String> parts. To remove individual parts, you can use remove() method, called like the add method, or to remove all elements, you can call clear()

    Putting this together:

    for (String part : c.parts) {
        this.parts.add(part);
    }
    c.parts.clear();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, NHibernate question here. I have two objects that I would like to map
I have two questions: (1) I learned somewhere that -O3 is not recommended with
I have two questions: 1) How can I make an array which points to
I have two questions. Do realloc() and memcpy() copy the entries in an array
I have two questions: 1) I have a few global variables for my website
I have two questions: From personal experience, what free blog engine is the best
I have two questions about java.awt.Shape . Suppose I have two Shape s, shape1
I have two questions 1) my interface i have interface called IRegister and within
I have two questions. 1) Should you always use a using statement on a
I actually have two questions regarding the same problem but I think it is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.