2 Replies - 599 Views - Last Post: 17 April 2014 - 10:33 PM Rate Topic: -----

#1 Zuruz   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 26-February 14

Strange Error From Method: What Does It Mean?

Posted 17 April 2014 - 10:04 PM

My array methods program is working well so far, but one method is still giving me an odd message. This method is supposed to search the array (which is integer based) for a target value, and then make and return a new array based on the indexes.

My array is {7,8,8,3,4,9,8,7} and the target value is 8, so the new array should be {1, 2, 6}.

According to my debugger, the method is allowing the information to be properly processed, but for some reason the print statement gives "[[email protected]".

What should I do to fix this? Also, what does the message mean? I assume it's an error message. Thanks a bunch.

Driver
import java.util.Arrays;
public class ArrayMethodsDriver
{
  public static void main(String[] args)
  {
    Integer[] a = {7,8,8,3,4,9,8,7};
  
     System.out.println("Create and print a new array with the instances of integer[] a when 8 occurs");
     System.out.println(ArrayMethods.findAll(a, 8));
    



Method

import java.util.Arrays;

public class ArrayMethods
{
public static int[] findAll (Integer[] a, int key)
   {
   int indexNum = 0;
   int arraySize = 0;
   
   for (int i = 0; i < a.length; i++)
   {
       if (a[i] == key)
       {
           arraySize++;
       }
   }
   int newArray[] = new int[arraySize];
   for (int i = 0; i < a.length; i++)
   {
       if (a[i] == key)
       {
           newArray[indexNum] = i;
           indexNum++;
       }
    }
   return newArray;
   }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Strange Error From Method: What Does It Mean?

#2 DimitriV   User is offline

  • vexing conundrum
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,746
  • Joined: 24-July 11

Re: Strange Error From Method: What Does It Mean?

Posted 17 April 2014 - 10:18 PM

The output that it prints is not an error. The reason you are getting what you are getting is because when toString is called on an array, it will return the memory address of the array instead of whatever it is that you wish to be output.
Arrays.toString(int[] a) is what you are looking for.

Information:

Quote

public static String toString(int[] a)
Returns a string representation of the contents of the specified array. The string representation consists of a list of the array's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (a comma followed by a space). Elements are converted to strings as by String.valueOf(int). Returns "null" if a is null.


So instead of:
System.out.println(ArrayMethods.findAll(a, 8));


You need:
System.out.println(Arrays.toString(ArrayMethods.findAll(a, 8)));


Was This Post Helpful? 2
  • +
  • -

#3 Zuruz   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 26-February 14

Re: Strange Error From Method: What Does It Mean?

Posted 17 April 2014 - 10:33 PM

Very informative!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1