Returning an array from a method

Hi i'm new to Java and i'm still in the basics.

I'm having trouble in a method, i need to return an array but everything i have tried doesnt work.

public static int suma(int[] array1, int[] array2) {

final int array_size = array1.length;

int array3 [];

array3 = new int[array_size];

for (int i = 0; i < array_size; i++)

array3 = array1 + array2;

return array3;

}

[451 byte] By [ByacoXa] at [2007-10-2 0:29:14]
# 1

When you post code, please use [code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.

public static int[] whatever() {

int[] result = ...;

...

return result;

}

jverda at 2007-7-15 16:43:42 > top of Java-index,Java Essentials,New To Java...
# 2
Please post your code again inside [code] [/code] tags.
YAT_Archivista at 2007-7-15 16:43:42 > top of Java-index,Java Essentials,New To Java...
# 3

public static int suma(int[] array1, int[] array2) {

final int array_size = array1.length;

int array3 [];

array3 = new int[array_size];

for (int i = 0; i < array_size; i++)

array3 = array1 + array2;

return array3;

}

sorry

ByacoXa at 2007-7-15 16:43:42 > top of Java-index,Java Essentials,New To Java...
# 4

That's better. Indentation would help though.

In any case, the answer was posted in reply 1. You forgot the [] in the declaration of the method's return type.

Also, in the future, if you post the error message the compiler gives you, it will make it easier for people to help you. And that message contains everything you need to know about what your mistake was. If you read that message carefully, you might be able to make an educated guess as to what you need to fix.

jverda at 2007-7-15 16:43:42 > top of Java-index,Java Essentials,New To Java...
# 5

thanks, i really apreciate your help.

I've got something else, I need to create a method that sums up each element of 2 matrix's and returns it in a third matrix. I'm having trouble creating the third matrix(wich needs to be the same size as the first two) and i'm also having trouble because i need to validate that the first two are equal in size.

ByacoXa at 2007-7-15 16:43:42 > top of Java-index,Java Essentials,New To Java...
# 6

int[] arr = ...;

int len = arr.length;

int[] anotherArr = new int[len];

The length field holds an array's length.

Do you know how to compare two ints to see if they're equal? Do you know how to do one thing if they are equal and another if they are not?

jverda at 2007-7-15 16:43:42 > top of Java-index,Java Essentials,New To Java...
# 7
Also note that you can't just add two arrays as if they're scalars. That kind of array arithmetic doesn't work in Java. You have to loop over the arrays, adding each pair of elements.
jverda at 2007-7-15 16:43:42 > top of Java-index,Java Essentials,New To Java...
# 8

i got no problem doing that with arrays, and yes i know to compare and i know if statements.

The problem is with multi-dimensional arrays and ragged arrays.

Lets see if this code can give you an ideas of what im asking:

public static int suma(int[][] matrix1, int[][] matrix2) {

final int array_sizex = matrix1.length;

final int array_sizey = matrix1[].length; // ? not sure how to do this

int matrix3 [][];

matrix3 = new int[array_sizex][array_sizey];

if ( matrix1.length != matrix2.length){ // i also need to check if they are equal in y size

System.out.println("Las matrices deben ser de igual tama駉");

return 0;

}

for (int x = 0; x < array_sizex; x++)

for (int y = 0; y < array_sizey; y++)

matrix3[x][y] = matrix1[x][y] + matrix2[x][y];

return matrix3;

}

ByacoXa at 2007-7-15 16:43:42 > top of Java-index,Java Essentials,New To Java...
# 9

> > final int array_sizey = matrix1[].length;

> 1[].length; // ? not sure how to do this

>

If the arrays are rectangluar, then just get matrix1[0].length and compare it to matrix2[0].length. It will be the same for all elements. Just the one check will be enough.

If they're ragged, then inside the outer loop you'll have to check if matrix1[x].length == matrix2[x].length.

jverda at 2007-7-15 16:43:42 > top of Java-index,Java Essentials,New To Java...