Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes summation of Nth element from multiple arrayLists for each element. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "summation of Nth element from multiple arrayLists for each element." Watch "summation of Nth element from multiple arrayLists for each element." New topic
Author

summation of Nth element from multiple arrayLists for each element.

Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 586
I have 30 arrayLists in a program. I need to do sum of 1st element of each arrayList,2nd element of each arrayList and so on. Had it been 2 arrayList it would have been possible by iterating one list within the another.Since the arrayLists are too many iterating one list within another does not look feasible. Please suggest what would be the best approach in this case.

Thanks.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
If all the arraylists have the same number of elements then you can do it with nested for loops. The outer loop for the number of elements and the inner loop for the number of arraylists.
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 586

Thanks but how to do this. I did not understand :
The outer loop for the number of elements and the inner loop for the number of arraylists.


Also I do not have 1 or 2 but 30 arrayLists.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7522
    
  18

Monica. Shiralkar wrote:Also I do not have 1 or 2 but 30 arrayLists.

So that might be a good place to start.

Why do you have 30 ArrayLists? Could this be increased to 50, or 100 or 1000? What would you do then?

If the likelihood is that this won't change, I'd suggest an array of ArrayLists. If it will, then probably an ArrayList of ArrayLists would be better.

But before you do anything, ask yourself: Why do I have 30 Lists?

I've never written a program with 30 lists in it - in any language - and I've been programming for 35 years.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 586
in hadoop the mapper emits key value pair and each value is arrayList of values against a given key..So once we have 30 arrayList how to proceed.Iterating one within another is not feasible solution.
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 586
Thanks.How would an ArrayList of ArrayLists help in doing this?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
Oh I see, you mean your 30 arraylists are separate variables and not an array or list of arraylists
The first thing to do is to change your code from dealing with individual arraylist variables to dealing with a collection of arraylists eg a List or array of arraylists and then you iterate over that.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7522
    
  18

Monica. Shiralkar wrote:in hadoop

A rather vital piece of information. In future, you might do well to remember stuff like this.

the mapper emits key value pair and each value is arrayList of values against a given key..So once we have 30 arrayList how to proceed.Iterating one within another is not feasible solution.

So, why not just process each ArrayList as it comes in? Or is there some other reason that makes you think that you need to gather them all together first?

I'm afraid we're still a few lines short of a full drawing here.

Winston
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 586
Thanks All.

The first thing to do is to change your code from dealing with individual arraylist variables to dealing with a collection of arraylists eg a List or array of arraylists and then you iterate over that.


Now suppose I have an array of arrayLists say 30 arrayLists.How do I Iterate the arrayList to get top 1 of each arrayList,then top 2 of each arrayList and so on? How would 1 iterator do that?



So, why not just process each ArrayList as it comes in? Or is there some other reason that makes you think that you need to gather them all together first?


It would arrive in Reducer class method as 30 arrayLists only in this case and calculation starts from there.So I need to find a way.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
Monica. Shiralkar wrote:
Now suppose I have an array of arrayLists say 30 arrayLists.How do I Iterate the arrayList to get top 1 of each arrayList,then top 2 of each arrayList and so on? How would 1 iterator do that?

I explained how to do that in my first reply.
Myke Enriq
Ranch Hand

Joined: Feb 13, 2012
Posts: 106
[Moderator action: Solution removed, for all the reasons mentioned by Tony below. - Winston]
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
@Myke Enriq: Please do not post complete solutions, the aim is to guide the OP so they can code the solution themselves.
And if you are going to post code please use code tags and provide code that compiles.
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 586
I am taking myArrayList.get[0] and summing all,next I am doing myArrayList.get[1] and sum all. This way it should work hopefully.

Thanks all.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7522
    
  18

Monica. Shiralkar wrote: I am taking myArrayList.get[0] and summing all,next I am doing myArrayList.get[1] and sum all. This way it should work hopefully.

You shouldn't hope, you should know.

Also, if you're simply processing myArrayList elements in sequence, there's a much better way to do it.
(Hint: ArrayList - indeed, all Java Collections - are Iterable (java.lang.Iterable ←click).

Winston
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: summation of Nth element from multiple arrayLists for each element.
 
Similar Threads
Options Tag not Recognizing Collection
Simple ArrayList conundrum?
ArrayList vs. LinkedList
I need help, with logic:iterate in HashMap
Pass 6 elements of arraylist to prepared stmt