Results 1 to 5 of 5
Thread: Returning array problem.
- 10-21-2008, 08:11 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 8
- Rep Power
- 0
Returning array problem.
I have an assignment that requires me to
The code I have so far is:# 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.
And the output I'm getting is:Java 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; } }
What is the problem with my code? Thanks for your help.[I@3e25a5
[I@19821f
- 10-21-2008, 08:19 PM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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 #3
This is java's way of outputing information about an object that does not have a special toString() method.[I@3e25a5
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 #4
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)
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 10-21-2008, 09:07 PM #5
Member
- Join Date
- Sep 2008
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
A problem in practicing the array
By dl21 in forum New To JavaReplies: 2Last Post: 04-24-2008, 11:32 PM -
array problem
By oceansdepth in forum New To JavaReplies: 3Last Post: 04-05-2008, 02:25 AM -
Array problem.. help needed please!
By SCS17 in forum New To JavaReplies: 3Last Post: 03-06-2008, 10:30 PM -
array problem
By wats in forum New To JavaReplies: 1Last Post: 12-12-2007, 07:08 AM -
array problem
By Albert in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 01:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks