wood burning stoves 2.0*
The moose likes Java in General and the fly likes Adding Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Adding Arrays" Watch "Adding Arrays" New topic
Author

Adding Arrays

sfsdfd sdfdsfgsdg
Greenhorn

Joined: Sep 02, 2009
Posts: 3
can someone show me or guide me in the right direction. I need to know how to create a method that has parameters of two primitive type arrays that must be added together and then returned in a separate array.

I really appreciate any and all suggestions on how to do this.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Do you have an approach already ?


JDBCSupport - An easy to use, light-weight JDBC framework -
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Hi, Welcome to the ranch
Don't forget to go through our FAQ, especially this one.


[My Blog]
All roads lead to JavaRanch
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
I have the result ready waiting for you, but come up with an approach first ...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Do you know how to create an array of a specific length? Do you know how to read elements from an array and assign array elements? If you answered "yes" to both questions, then it shouldn't be hard at all.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Rob Prime wrote:Do you know how to create an array of a specific length? Do you know how to read elements from an array and assign array elements? If you answered "yes" to both questions, then it shouldn't be hard at all.


There is however a nice generic approach that is a little trickier for starters.
sfsdfd sdfdsfgsdg
Greenhorn

Joined: Sep 02, 2009
Posts: 3
yea im pretty new to java.
sfsdfd sdfdsfgsdg
Greenhorn

Joined: Sep 02, 2009
Posts: 3

public class addingArray {

public static void main(int [] one, int [] two) {


if (one.length > two.length) {
int [] resultArray = new int [one.length];

}
if (one.length > two.length) {
int [] resultArray = new int [two.length];

}

for (int i=0; i<one.length;i++) {
resultArray[i] = one[i] + two[i];

}

System.out.println( resultArray);




}


}
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Hey,

please put your code between the code tags to makes things more readable .

I got you wring earlier. I thought you wanted to merge two arrays together returning one array that contains the accumulated array values.

As for your approach, that looks fair. But I think you do not want to call this method public static void main(int [] one, int [] two) ;)

You permit clients to pass two arrays of different lengths in there, is that what you want or should the array sizes match?

In that case you can impose this restriction by validating before you do any computations.




If you want to take it one step further, you could accept a variable number of arrays coming in.
 
GeeCON Prague 2014
 
subject: Adding Arrays