Results 1 to 5 of 5
  1. #1
    Chase is offline Member
    Join Date
    Sep 2008
    Posts
    8
    Rep Power
    0

    Question Returning array problem.

    I have an assignment that requires me to

    # 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:

    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;
        }
    }
    And the output I'm getting is:

    [I@3e25a5
    [I@19821f
    What is the problem with my code? Thanks for your help.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    to print the items of an array, you must iterate through them. you can't simply print the object.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    [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.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default 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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    Chase is offline Member
    Join Date
    Sep 2008
    Posts
    8
    Rep Power
    0

Similar Threads

  1. A problem in practicing the array
    By dl21 in forum New To Java
    Replies: 2
    Last Post: 04-25-2008, 12:32 AM
  2. array problem
    By oceansdepth in forum New To Java
    Replies: 3
    Last Post: 04-05-2008, 03:25 AM
  3. Array problem.. help needed please!
    By SCS17 in forum New To Java
    Replies: 3
    Last Post: 03-06-2008, 11:30 PM
  4. array problem
    By wats in forum New To Java
    Replies: 1
    Last Post: 12-12-2007, 08:08 AM
  5. array problem
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 02:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •