We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,676 Members — Technology Publication meets Social Media

# Sum of ArrayList<Integer> values, recursion

I'm trying to add the int values of Integer objects within an ArrayList, using a recursion (for the first time). Here's what I have so far:

``````import java.util.Scanner;
import java.util.ArrayList;

public class SumArrayList {
private static int calculateSumArrayListHelper(ArrayList<Integer> duplicate) {
if (duplicate.size() == 0) {
return 0;
}
int lastNum = duplicate.get(duplicate.size()-1).intValue();
return lastNum + calculateSumArrayListHelper(duplicate.remove(duplicate.size()-1));
}

public static int calculateSumArrayList(ArrayList<Integer> original) {
ArrayList<Integer> duplicate = (ArrayList<Integer>) original.clone();
return calculateSumArrayListHelper(duplicate);
}

public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<Integer>();
System.out.println("How many values should the ArrayList contain?");
int arrayLength = reader.nextInt();
System.out.println("Enter "+arrayLength+" integers:");
for (int i = 0; i < arrayLength; i++) {
Integer j = new Integer(reader.nextInt());
}
System.out.println("The sum of all the integers in that ArrayList is: "+calculateSumArrayList(list));
}
}
``````

And of course, the compile error:

``````SumArrayList.java:10: calculateSumArrayListHelper(java.util.ArrayList<java.lang.Integer>) in SumArrayList cannot be applied to (java.lang.Integer)
return lastNum + calculateSumArrayListHelper(duplicate.remove(duplicate.size()-1));
^
SumArrayList.java:10: operator + cannot be applied to int,calculateSumArrayListHelper
return lastNum + calculateSumArrayListHelper(duplicate.remove(duplicate.size()-1));
^
SumArrayList.java:10: incompatible types
found   : <nulltype>
required: int
return lastNum + calculateSumArrayListHelper(duplicate.remove(duplicate.size()-1));
^
Note: SumArrayList.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
3 errors
``````

First of all, I don't know why I'm getting the "Notes" because I'm pretty sure I've specified what type of object goes into each ArrayList that I've created. Second, why does the computer think that `duplicate.remove(duplicate.size()-1)` is an Integer? All I'm doing is removing the last element of the ArrayList, so it should still be an ArrayList, right? It's getting reaaallly hard to remain calm with these programs...

P.S. I know I've posted in this forum a lot lately, so I just wanted to say thank you to everyone who has helped me, and thank you in advance to those who will help me!

5
Contributors
9
Replies
2 Days
Discussion Span
10 Months Ago
Last Updated
12
Views
Question
sammoto
Light Poster
41 posts since May 2012
Reputation Points: 14
Skill Endorsements: 0

All I'm doing is removing the last element of the ArrayList, so it should still be an ArrayList, right?

No, thats wrong !!

remove is a method that returns the element that was removed from the list. Check this and in this case it is Integer !
I beleve you should redesign your method.

naief
Light Poster
36 posts since Nov 2010
Reputation Points: 13
Skill Endorsements: 0

Do something like:

``````//...
int lastNum = duplicate.remove(duplicate.size()-1);
return lastNum + calculateSumArrayListHelper(duplicate);
``````
Krokcy
Junior Poster in Training
55 posts since Sep 2010
Reputation Points: 24
Skill Endorsements: 0

THANK YOU!!!! You guys spared me a HUGE headache!

sammoto
Light Poster
41 posts since May 2012
Reputation Points: 14
Skill Endorsements: 0
Question Answered as of 10 Months Ago by Krokcy and naief

I think this may actually help solve another issue I'm having with ArrayLists in another program

sammoto
Light Poster
41 posts since May 2012
Reputation Points: 14
Skill Endorsements: 0

i don't think your clone method works, might wanna check that

curiousgeorgem
Newbie Poster
4 posts since Jun 2012
Reputation Points: 0
Skill Endorsements: 0

i don't think your clone method works, might wanna check that

Can you clarify that? ArrayList certainly implements a shallow clone(), so what do you suspect, and why?
Thanks.

JamesCherrill
... trying to help
Moderator
8,504 posts since Apr 2008
Reputation Points: 2,583
Skill Endorsements: 29

i tried compiling it but it gave me a complier error. I'm not sure why
it said unchecked cast from java lang object to java until arraylis<java lang integer>

curiousgeorgem
Newbie Poster
4 posts since Jun 2012
Reputation Points: 0
Skill Endorsements: 0

Yes, clone is defined as returning an Object, so you are stuck with the cast. What version of Java are you using? Eclipse 4.2/Java 1.7 doesn't complain about it.

JamesCherrill
... trying to help
Moderator
8,504 posts since Apr 2008
Reputation Points: 2,583
Skill Endorsements: 29

I'm actually using the newest version of java, which is weird. if its working for you then ill take your word for it :P

curiousgeorgem
Newbie Poster
4 posts since Jun 2012
Reputation Points: 0