Results 1 to 6 of 6
- 10-29-2010, 04:32 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 5
- Rep Power
- 0
How do I write a method that returns an array where ith element contains a[i] + b[i]?
How do I write a method that returns an array where ith element contains a[i] + b[i]?
This is what I have so far:
public class NewClass
{
public static void main(String[] args)
{
int[] res = new int[5];
int[] arraySum = new int[5];
int[] a = {1, 2, 3, 4, 5};
int[] b = {0, 1, 2, 3, 4};
res = arraySum(a, b);
System.out.println("The array is: " + res);
}
public static int[] arraySum(int[] a, int[] b)
{
int[] arr = new int[5];
for(int i = 0; i < arr.length; i++)
{
arr[i] = a[i] + b[i];
}
return arr;
}
}
But in the output I get:
The array is: [I@2e6e1408
What should I change?
Thanks ;)
- 10-29-2010, 04:42 AM #2
Change your output statement. You need to run through each part of the array to display it on the screen.
Java Code:for(int i = 0; i < res.length; i++) System.out.println(res[i]);Sincerely, Joshua Green
Please REP if I help :)
- 10-29-2010, 04:47 AM #3
Member
- Join Date
- Aug 2010
- Posts
- 5
- Rep Power
- 0
- 10-29-2010, 04:52 AM #4
No problem, arrays can be tricky at first.
Sincerely, Joshua Green
Please REP if I help :)
- 10-29-2010, 09:11 AM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
To expand on the topic at hand, the reason you get [I@2e6e1408 as an output, is because that is the memory location your array variable references, and it also is the default behavior when trying to print out objects. To avoid these kind of problems in the future with your own classes, always override the toString() method:
Java Code:public class Output1 { private int x; public Output1(int x) { this.x = x; } } class Output2 { private int x; public Output2(int x) { this.x = x; } public String toString() { return x+""; //the +"" part is to convert x from int to String } } class OutputTest { public static void main(String[] args) { Output1 o1 = new Output1(2); Output2 o2 = new Output2(2); System.out.println(o1); //here you get something similar as your problem System.out.println(o2); //here the output is 2 } }Ever seen a dog chase its tail? Now that's an infinite loop.
- 10-30-2010, 12:05 AM #6
Member
- Join Date
- Aug 2010
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
What is the Java library method that takes String parameter and returns a Boolean?
By CaptainBlood in forum New To JavaReplies: 2Last Post: 10-15-2010, 05:09 AM -
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM -
Max element in an Array
By mew in forum New To JavaReplies: 5Last Post: 12-03-2007, 05:26 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks