• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Adding Arrays

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have an approach already ?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Welcome to the ranch
Don't forget to go through our FAQ, especially this one.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the result ready waiting for you, but come up with an approach first ...
 
Sheriff
Posts: 22716
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yea im pretty new to java.
 
sfsdfd sdfdsfgsdg
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic