3

As part of an assignment I'm supposed to create a bunch of rectangles and put each of those objects within an ArrayList, which is to be initialized in a constructor. I think I've confused myself. Eclipse is telling me that I can't use .add(new_rec) with this object because it is of type (Double, Double). I assume because the ArrayList is simply <Double> this is causing some issue. Aside from that I think I'm misguided somewhere else, but I'm not sure where.

public class WhyWontThisWork {

        WhyWontThisWork(Double name, Double rec_name){
            Rectangle new_rec = new Rectangle(23.1,43.0);
            ArrayList<Double> name = new ArrayList<Double>();
            for(int i = 0; i < 10; i++){
                name.add(new_rec);
            }
        }
}

...and the Rectangle class looks like...

public class Rectangle {
    private double length;
    private double width;

    public Rectangle(double length, double width){
        this.length = length;
        this.width = width;
    }
3
  • 2
    the main reason is that a rectangle is not a double, additionally, you have to give different names for name, they cannot be the same Commented Jan 31, 2014 at 7:58
  • also variable namealready defined in the scope Commented Jan 31, 2014 at 7:59
  • @epoch Oops, my mistake on the naming sloppiness. Thanks for pointing that out. Commented Jan 31, 2014 at 8:00

6 Answers 6

2

This is really quite simple- the type you need for name should be:

ArrayList<Rectangle> name = new ArrayList<Rectangle>();

The generic type inside the <> represents what kinds of a thing you are adding to the ArrayList. If you need to store an array of double you can use ArrayList<Double> if they are strings then ArrayList<String>

In your case you need to store a list of Rectangles so ArrayList<Rectangle> is your solution.

Sign up to request clarification or add additional context in comments.

5 Comments

So the <...> is to be filled with any object? I'm confused on why Rectangle works here.
Updated with more explanation - hope this helps.
So if I created a class of, say, Kittens and I needed to fill an ArrayList with those objects would I declare ArrayList<Kittens>?
Exactly - this is what Java generics are for - giving you a type-safe way to implement algorithms (such as linked lists/array lists) which are not specific to a particular data type.
Better use interfaces and not the concrete implementation class for name, i.e. List<Rectangle> name = new ArrayList<Rectangle>();
2

ArrayList used need to be

ArrayList<Rectangle> name = new ArrayList<Rectangle>();

As you are using the arrayList to save Rectangles not Doubles.

Comments

0

Your arraylist must be of type Rectangle to be able to do this.

ArrayList<Rectangle> name = new ArrayList<Rectangle>()

Comments

0

Eclipse is right.

ArrayList<Rectangle> name = new ArrayList<Rectangle>();
name.add(new_rec);

Comments

0

Also parameter and ArrayList name is same,change it as below :

   WhyWontThisWork(Double nameDouble, Double rec_name){
                Rectangle new_rec = new Rectangle(23.1,43.0);
                ArrayList<Rectangle> name = new ArrayList<Rectangle>();
                for(int i = 0; i < 10; i++){
                    name.add(new_rec);
                }
            }

Comments

0

You are trying to add a Rectangle object to a ArrayList that holds Double objects. That is why you see the error. As pointed out in the other answers the likely best solution is to change the list to ArrayList<Rectangle>.

Maybe you should read a bit about generics in Java here or here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.