Question

The following is the "main" body of work-doing code:

public class ArrayFunHouse
{
    //instance variables and constructors could be used, but are not really needed

    //getSum() will return the sum of the numbers from start to stop, not including stop
    public static int getSum(int[] numArray, int start, int stop)
    {
        int sum = 0;

        for(int count = start; count <= stop; count++) {
            sum += numArray[count];
        }
        return sum;
    }

    //getCount() will return number of times val is present
    public static int getCount(int[] numArray, int val)
    {
        int present = 0;

        for(int count = 0; count < numArray.length; count++) {
            if(numArray[count] == val) {
                present++;
            }
        }
        return present;
    }

    public static int[] removeVal(int[] numArray, int val)
    {
        int[] removal = new int[numArray.length - getCount(numArray, val)];
        int arbitrary = 0;

        for(int count = 0; count < removal.length; count++) {
            if(numArray[count] == val) {
                arbitrary++;
            } else {
                removal[count - arbitrary] = numArray[count];
            }
        }

        return removal;
    }
}

And the runner class:

import java.util.Arrays;

public class ArrayFunHouseRunner
{
public static void main( String args[] )
{
    int[] one = {4,10,0,1,7,6,5,3,2,9};

    System.out.println(Arrays.toString(one));
    System.out.println("sum of spots 3-6  =  " + ArrayFunHouse.getSum(one,3,6));
    System.out.println("sum of spots 2-9  =  " + ArrayFunHouse.getSum(one,2,9));
    System.out.println("# of 4s  =  " + ArrayFunHouse.getCount(one,4));
    System.out.println("# of 9s  =  " + ArrayFunHouse.getCount(one,9));
    System.out.println("# of 7s  =  " + ArrayFunHouse.getCount(one,7));
    System.out.println("new array with all 7s removed = "+     ArrayFunHouse.removeVal(one, 7));


}
}

Returns something that I would expect from attempting to print a class without a toString, i.e. this:

[4, 10, 0, 1, 7, 6, 5, 3, 2, 9]
sum of spots 3-6  =  19
sum of spots 2-9  =  33
number of 4s  =  1
number of 9s  =  1
number of 7s  =  1
new array with all 7s removed = [I@56f7ce53

I know that I called it correctly in the runner, there's not much there to screw up, can't pinpoint the issue, any advice?

Final edit: Well that's embarrassing. The issue was in fact in the runner, not calling an Arrays.toString.

Was it helpful?

Solution

print with

System.out.println(Arrays.toString(array));

and if your array has array inside.

System.out.println(Arrays.deepToString(array));

OTHER TIPS

As you stated, you are printing the toString() of the returned array. However, in Java, arrays don't override toString(), hence the output you're getting. Instead of just printing the returned value, use Arrays.toString(int[]) to print it's value - that should give you the desired output.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top