# Returning array problem.

• 10-21-2008, 08:11 PM
Chase
Returning array problem.
I have an assignment that requires me to

Quote:

# Part A:

1. Create a function called replaceFifteen, that takes an array of integers as a parameter, and has a void return type
2. Write code inside replaceFifteen, so that it replaces every occurance of 15 in the input array with -1.
3. In your main function, create an integer array a and set it to {2, 4, 8, 15, 49, 63, 47}
4. Call replaceFifteen and pass a as a parameter.
5. Print the resulting array

# Part B:

1. Create a function called "reverseArray", that takes an array of integers as parameter, and returns a new array.
2. The array that reverseArray returns must have its elements in the reverse order of the array that was passed to it.
3. Create an integer array {2, 5, 7, 4, 8}. Pass it to reverseArray.
4. Print the resulting array. (The output should be 8, 4, 7, 5, 2).
5. reverseArray must not modify the array that was passed to it.

The code I have so far is:

Code:

```/**  * Chase Spell  * Lab 8  * Oct. 21, 2008  */ public class Lab8 {         /**     * @param args the command line arguments     */     public static void main(String[] args) {         //creates an integer array a and set it to {2, 4, 8, 15, 49, 63, 47}         int[] a = {2,4,8,15,49,63,47};         //calls replaceFifteen and passes a as a parameter         replaceFifteen(a);         //prints a         System.out.println(a);                 //creates an int array b         int[] b = {2,5,7,4,8};         System.out.println(reverseArray(b));     }         //replace fifteen function     public static void replaceFifteen(int[] arr) {             //replaces every value of 15 with -1             for (int i = 0; i < arr.length; i++) {                     if (arr[i] == 15)                             arr[i] = -1;             }//end for     }         //reverse array     public static int[] reverseArray(int[] arr) {             int[] temp = new int[arr.length]; //temp array used to reverse the input array             int position;//used to reverse             for (int i = 0; i < arr.length; i++) {                     position = arr.length - i;                     temp[i] = arr[position - 1];             }             return temp;     } }```
And the output I'm getting is:

Quote:

[I@3e25a5
[I@19821f
What is the problem with my code? Thanks for your help.
• 10-21-2008, 08:19 PM
emceenugget
to print the items of an array, you must iterate through them. you can't simply print the object.
• 10-21-2008, 08:32 PM
Norm
Quote:

[I@3e25a5
This is java's way of outputing information about an object that does not have a special toString() method.
Arrays are treated like objects which don't have the toString method.
The leading [ means that the object is an array
the I means contents are int
the @xxxxx gives the address in memory where the object is.

None of this is of any use to anyone. But when you did println(a) that is what java will give you.
See the above message if you want the contents of the array.
Also look at the Arrays class. It might have methods to display the contents of an array.
• 10-21-2008, 08:43 PM
CJSLMAN
Scope of the array...
Also, when you do print your array, you're not going to see the -1 in it. You have a scope problem with the array. Your original array (that contains the 15) is in the scope of the main method. The second array (that contains the -1) is in the scope of the replaceFifteen () method. Therefore you have two separate arrays (in two separate methods). There are two ways you can fix it:
• Print the modified array from the replaceFifteen () method or
• Declare your array (arr[]) as global (declare it before the main () method)

CJSL
• 10-21-2008, 09:07 PM
Chase
It's working fine now. Thanks everyone.