The Artima Developer Community
Sponsored Link

Java Answers Forum
how do u add two arrays

1 reply on 1 page. Most recent reply: Nov 14, 2002 9:09 PM by Dharmendra

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Cshah

Posts: 16
Nickname: tkc204
Registered: Oct, 2002

how do u add two arrays Posted: Nov 14, 2002 8:57 PM
Reply to this message Reply
Advertisement
A "sum" method which takes another IntVector as a parameter, and returns a new
IntVector which is the element-wise sum of the two original objects. For example,
if an IntVector object contains the integers { 4, 10, 7, 2 }, and you call
"sum" passing as an argument another IntVector which contains { 1, 2, 3, 4 },
the result should be an IntVector which contains { 5, 12, 10, 6 }.
This method will have to check to make sure the arrays in the invoking object
and the parameter have the same length.


public static void sum(int [] numbers)
{
int[] numbers2 = {1,2,3,4};
int[] numberSum= {0,0,0,0};

//numberSum.length = numbers.length;
for(int i = 0; i < numbers.length-1; i++)
{
for (int j = 0; j< numbers2.length-1; j++)
{
numberSum[j] = numbers2[j] + numbers;
}

}
System.out.println(numbers2);

}


Dharmendra

Posts: 10
Nickname: dh547
Registered: Oct, 2002

Re: how do u add two arrays Posted: Nov 14, 2002 9:09 PM
Reply to this message Reply
Since u have to check for their size equality.You dont have to use two loops.

for(i=0;i<size;i++)
num3=num1+num2;

there is no vecmath package in java.
ofcourse there are third party pacjages

Flat View: This topic has 1 reply on 1 page
Topic: EJB,JSP,Servlets Interview questions? Previous Topic   Next Topic Topic: How to set User-Agent?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use