This week's book giveaway is in the Design forum.We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!

# How to add double into an ArrayList?

Kimberly Ivens
Greenhorn
Posts: 9
Java beginner trying to understand Arrays and ArrayLists. Here is the problem for my homework and the code I have created for it:
_______________________________________________
Problem 1. Write a class that performs math operations on elements in an array of type double and size 10.
a. The constructor will accept as input an array of double. (in your parameter it should say double[ ] name)
b. There should be a method the will return the difference between the largest and the smallest elements in the array of doubles.
c. There should be another method that returns the sum of all the elements.
d. Write another method that returns the number of elements above a value read in from the user in the tester class. You should send an explicit parameter to the method and compare each element in the array to this value.
e. There should also be a tester class that asked the user to input 10 values and put them in an array. The tester class should create an object of the math operations class and call each of the methods. The tester class should print all the values returned from the methods.

Problem 2. Redo the above problem but use Array Lists instead.
_____________________________________________________

I have Problem 1 completed and working using an array.
In problem 2 I am having trouble converting the array to an array list and making the program work.
I do not fully understand how array lists are supposed to be constructed. Here is my code for the CLASS and the TESTER CLASS:

In the tester class, I have created a new ArrayList called arry1. I need to fill it with 10 values. I know that an array list cannot accept primitive types like doubles. This is where I get confused. I think I'm supposed to make a new object of my class and use that object to hold a value in my array list. Is this correct? The problem (I think) is where I have noted //----?? in the tester class code. I don't know how to make the arrayList accept input from the user. I don't know the code format for doing that.

Please help. I don't understand what I'm doing and our text and instructor covered this too fast and did not explain how to make this work. Please keep in mind that this is the most advanced Java I know, so please don't confuse me further with language or suggestions beyond my level of understanding. I'm new.

Thanks so much!

Kemal Sokolovic
Bartender
Posts: 825
5

Kimberly Ivens
Greenhorn
Posts: 9
This post I also asked on another site, but not one answer was given to help me and my question got lost amongst hundreds of other questions. I had asked a question here before and got very nice responses and much help. I forgot my password here and posted it there, but then remembered my password here, so I posted it here. Sorry if I did something wrong.

Kemal Sokolovic
Bartender
Posts: 825
5
Your question on the other forum was asked about 2 hours ago. That means you don't have patience to wait long enough to get an answer. What makes you think anyone here will give you the answer quicker? People on forums are volunteers and give answers when they find some time off.

What is wrong with crossposting (and not being forthright about it) is that in such case the OP (in this case you) is the only one that can benefit. You might get an answer sooner or later, at one forum or another. But that means that only one answer will satisfy your issue, hence all the other people that were involved in trying to help you were wasting their time. And wasting time is not something anyone likes to do.

I had asked a question here before and got very nice responses and much help.

And you always will, if you in turn try to follow some basic rules this forum has (How To Ask Questions On Java Ranch).

Kimberly Ivens
Greenhorn
Posts: 9
I never thought of it in that way before. Thank you for correcting my error. I will remember your advice when posting in the future. Forgive my novice mistake. I had assumed that if I posted in that forum and this one that an answer (that I could actually understand) may be given on at least one of them. I had searched and searched on many sites looking for an answer on how to fill an array list with doubles, but the other sites were not using a Scanner input and a for loop or a class/tester setup. I am a java novice at best, so I didn't understand how to adapt coding I found to match what I needed. I am new to posting questions in java forums and I did not take time to consider the etiquette of posting. Again, thanks for taking time to explain that concept.

Praveen Kumar M K
Ranch Hand
Posts: 256
Kimberly Ivens wrote:

In the tester class, I have created a new ArrayList called arry1. I need to fill it with 10 values. I know that an array list cannot accept primitive types like doubles. This is where I get confused. I think I'm supposed to make a new object of my class and use that object to hold a value in my array list. Is this correct? The problem (I think) is where I have noted //----?? in the tester class code. I don't know how to make the arrayList accept input from the user. I don't know the code format for doing that.

In the program above, have a look at line 22,

ArryMathOps_2 arryObj = new ArryMathOps_2(arry1);

Here you are trying to pass "arry1" to the constructor of ArryMathOps_2. This "arry1" is a variable of the type ArrayList<ArryMathOps_2>, but the constructor is actually expecting ArrayList<Double>. So what does that tell you?

Kimberly Ivens
Greenhorn
Posts: 9
At line 22...trying to pass "arry1" to the constructor of ArryMathOps_2 and it's expecting a ArrayList<Double>.

I am attempting this code in BlueJ, and the code hangs up at line 17, so it goes no further than that. And I haven't changed the rest of the code because I couldn't get line 17 to work yet.

But since line 22 is trying to get a double and it's getting an array, then I guess I need to change the class constructor to accept an array?

Praveen Kumar M K
Ranch Hand
Posts: 256
Nope thats not exactly what I intended you to understand. Lets have a look at your code again,

1) ArrayList<ArryMathOps_2> arry1 = new ArrayList<ArryMathOps_2>();

What is the intention of this line? Here we are creating an ArrayList called arry1 and we are telling that we'll be storing elements of type ArryMathOps_2 (and not Double)

2) arry1.add(arryObj[i], aValue);

arryObj is not an array reference, so how can you use a array construct like arrayObj[i]. There must be a corresponding compiler error for this.

3) For the same code line as above, is there an add method in API of ArrayList(<-- click this link) which takes in these input parameters?

4) Now finally, back to ArryMathOps_2 arryObj = new ArryMathOps_2(arry1);

The constrcuctor of the class ArryMathOps_2 is looking for a ArrayList<Double> variable as input. Now wouldnt it be wrong to give it a variable of type ArrayList<ArryMathOps_2>?

Kimberly Ivens
Greenhorn
Posts: 9
1) ArrayList<ArryMathOps_2> arry1 = new ArrayList<ArryMathOps_2>();
What is the intention of this line? Here we are creating an ArrayList called arry1 and we are telling that we'll be storing elements of type ArryMathOps_2 (and not Double)

---->>There is only one example I was given in how to create an array list from a program that had a class of Employee like this:
PayrollClass[] payroll = new PayrollClass[10];
then a for loop and inside it that had
PayrollClass employee = new PayrollClass(id, hours, pay);
payroll[i] = employee;
That is the only example I had to learn from. From what I understand, an arrayList does not accept double input, only objects, so I thought I had to somehow use the class as a type to make the array (arry1). I don't understand what I am supposed to do. Should I change it from double[] in the constructor or should I change the code here to <double>? My inputs will be double values.

2) arry1.add(arryObj[i], aValue);
arryObj is not an array reference, so how can you use a array construct like arrayObj[i]. There must be a corresponding compiler error for this.
---->Yes, the compiler gives an error. My text explains that Array.add() will accept an object or an array and it gives an example of adding elements to an array by using Array.add([location], input). But I didn't quite understand how to tell the .add command to use each index number and put the corresponding value into it. This is what I thought it meant to do.

3) For the same code line as above, is there an add method in API of ArrayList(<-- click this link) which takes in these input parameters?
----->The .add method for ArrayList takes parameters in this way: Array(0, Sue) that changes the location at 0 to "Sue." That was the only example I was given. I thought it would work by using an array object with index and the values inserted...but I haven't done much with that part yet because I didnt get past line 17 in compiler.

4) Now finally, back to ArryMathOps_2 arryObj = new ArryMathOps_2(arry1);
The constrcuctor of the class ArryMathOps_2 is looking for a ArrayList<Double> variable as input. Now wouldnt it be wrong to give it a variable of type ArrayList<ArryMathOps_2>?
----->I need to put doubles in the arrayList, but arrayList doesn't accept doubles, only objects. I was attempting to follow my example program in some type of "wrapper" class thing that I don't completely understand either. I'm so sorry. I really have been working on this for 3 days. I don't have anyone to help me off campus who knows programming at all, and we only went over array/array lists for about 45 minutes with an example of each. Using scanner input and using an array as a parameter in a constructor or method wasn't even discussed and now it's homework. I'm so grateful to have this forum or I wouldn't be passing at all.

I thought I was supposed to create an object of the class using the arry1 as input to it to accept the values. Am I completely off? Have I done anything right in this program at all?
This same program I created that use an array (not array list) works fine. It's just doing it as an arrayList I totally do not understand. What exactly has to change from array to arrayList?

Praveen Kumar M K
Ranch Hand
Posts: 256
Ok. Easy!

Lets go over what you need to do.

1) You need to accept user input, a number in this case and you need to store that in a ArrayList.

Lesson1 : While defining ArrayList references, you need to specify that type of the values that the ArrayList can contain(lets forget about the "generic" type for now). You can create ArrayLists of type Integer, Float, Double, Object...etc. When you create an ArrayList of type X, you can only put in values of type X into it. If you break this rule, compiler will throw up an error. Ok?

Now, corresponding to my first point in previous post, what you have done is created an ArrayList of type ArryMathOps_2. Now in such a list, you can only store elements of type ArryMathOps_2. But what you really need, is to create an ArrayList of Double and put in values of Double. Here aValue can be Double or a double because of a concept called Autoboxing. The compiler will automatically convert a plain double literal to an object Double and store it into the list. You are right in saying that ArrayList can contain only objects and not plain literals.

Now I presume that you know what type of ArrayList needs to be created.

2) Next, you need to loop 10 times, accept the double value in each loop and store that value in the ArrayList of Double.

Lesson2 : ArrayList API says that it has 2 basic add methods. One which requires an index and one that doesn't.

public boolean add(E e)
Appends the specified element to the end of this list.

and

public void add(int index, E element)
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

You are using the second case. You have the index "i" which ranges from 0 to 9 and you have the element namely the double value aValue.

So essentially your add call should be like arry1.add(i, aValue); Right? Line 17 is of no use.

3) Now for the final step. The arry1 ArrayList of Double needs to be assigned to a member variable(arrayData) in the class ArryMathOps_2 and we have a constructor to do just that. The constructor is defined as below,

Now if you've followed the change in step 1 properly, the line

should not throw an error. Why? Because the constructor is looking for a ArrayList<Double> and arry1 would also be an ArrayList<Double>. This should essentially solve your errors and you should be able to run it smoothly.

I suggest you go through the nuts and bolts of Generics and then try solving the problem. Good luck!

Kimberly Ivens
Greenhorn
Posts: 9
• 1
It works! Wow!

After you explained it so completely and thoroughly, I understand everything you said and exactly what to do, BUT MORE IMPORTANTLY I now understand why it has to be that way.

I studied this code very carefully and compared it to code I was given as an example. The difference was that my assignment was to use the ArrayList as a parameter to a constructor. The example program only used variables as input to the constructor and used those variables to fill an ArrayList. I understand that I was not creating my ArrayList correctly. It had to accept a double as input, so I had to create it as a double (duh). Then when I created the object and put the array in it, then it worked. Well, I understand it in my head although I can't seem to relay my understanding very well in this venue.

Again, I am very grateful for your "teaching" style and very clear explanations detailing the code that I had erred. I learned alot by the way you guided me to the answer without revealing too much so that I could think out the answer.

My sincerest gratitude to you for your time and attention and your teaching style is excellent. You are truly an asset to this community.

Praveen Kumar M K
Ranch Hand
Posts: 256
Thank you and you are welcome

Campbell Ritchie
Sheriff
Posts: 48652
56
Please read this and don’t post such long comments. I have had to break the lines because they are hard to read. They are also in the wrong format, starting **.
I have also deleted some of the code which had been quoted, because it made the replies too long. Praveen Kumar M K, please don’t be annoyed about it, and thank you for the work you put into the reply.

Please post on the other forum that you have had a reply here; the bit about posting on other sites applies to the people there, too.