dsorenson
dsorenson

Reputation: 11

toString method with adjustable Array size

Every source I've looked at I either don't understand, doesn't seem to apply, or uses something like an Array list. I'm not familiar with those. So I'd like to use a basic toString method that prints out the index of the array as well as the number held when compared to the variable 'length' -- num.length could be different as that's the physical size of the underlying array. The for loop at the bottom has the gist of it. I'm trying to print out the index (0-infinite) with int's that are held in the resizeable array. The variable 'length' is not the actual size of the array but a working size that contains 0 until another cell is added. The 'strang' variable is just something I've tried. I don't think it will work, but anything else I doesn't seem to help as I'm stuck.

public class XArray
{
    private int[] nums;
    private int length;

    public XArray()
    {
        nums = new int[10];
        length = 0;
    }

    public void add(int value)
    {
        if (length == nums.length)
        {
            int[] nums2 = new int[(int)(nums.length * 1.2)];
            for ( int i = length - 1; i >= 0; i-- )
            {
                nums2[i] = nums[i];
            }
            nums = nums2;
        }
        nums[length] = value;
        length++;
    }

    public void set(int index, int value)
    {
        if (index < length)
        {
            nums[index] = value;
        }
    }

    public int get(int index)
    {
        return nums[index];
    }

    public int size()
    {
        return length;
    }

    public void remove()
    {
        nums[length - 1] = 0;
        length--;
    }

    public String toString(int[] nums)
    {
        String strang = "l";
        for ( int i = 0 ; i < length; i++ )
        {
            strang = "Index: " + i + " Number: " + nums[i] + ", ";
        }
        return strang;
    }
}

Upvotes: 0

Views: 1225

Answers (2)

Ray Toal
Ray Toal

Reputation: 88378

You probably meant to use += instead of = in that method (though many people will tell you to use a StringBuilder because successive concatenations, if not optimized by a compiler` will generate a lot of garbage).

Also, don't pass in nums! You want to use the field nums; passing in an argument will use the argument. The real toString has no parameters (and should have an @Override annotation).

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347184

You need to concatenate the values on each iteration of the loop...something like...

public String toString(int[] nums)
{
    StringBuilder strang = new StringBuilder(length);
    for ( int i = 0 ; i < length; i++ )
    {
        strang.append("Index: ").append(i).append(" Number: ").append(nums[i]).append(", ");
    }
    return strang.toString();
}

Generally speaking, toString should't take parameters, there would a difference between nums and length which could cause issues

@Override
public String toString() {...

This way, you will be printing the contents of the objects num array, which is contextually associated with length

Upvotes: 1

Related Questions