File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding Arrays

 
sfsdfd sdfdsfgsdg
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have an approach already ?
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the result ready waiting for you, but come up with an approach first ...
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yea im pretty new to java.
 
sfsdfd sdfdsfgsdg
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic