Hatz
Hatz

Reputation: 23

Printing an array with no elements?

I have a few questions. I am working on a homework assignment but came across a few confusing things. (I am taking an introductory class, sorry for the mistakes.)

Implement a class that takes an integer array and an int x as its size. Create a method inside the class that creates a new array whose length is one greater than data’s length. Then create a method to copy all data’s elements into the new array and add the value of x into the last element of the array. Create a method to return all the integers in the new array.

Here's what I have

package taskone;

import java.util.*;

class Arrayplus1 {

    int x;
    int data[];

    void example(int x) {
        this.x = x+1;
        this.data= new int[x];
    }

    void increaseSizeOfArray(int incrementSize) {
            int copiedArray[] = Arrays.copyOf(data, data.length + incrementSize);
            data = copiedArray;

    }

    void printall() {
        System.out.println(Arrays.toString(data));
    }
}

public class TaskOne {

    public static void main(String[] args) {
        example task = new example();
        task.printall();
    }

}

This simply returns null.

My question is the last bit of the task above. "Create a method to return all the integers in the new array."

How is this possible when I did not include any elements in my array? I am trying to follow the assignment guidelines. Thanks.

Upvotes: 2

Views: 3034

Answers (3)

Christian Meyer
Christian Meyer

Reputation: 625

You are making a simple mistake here:

In:

public class TaskOne {

    public static void main(String[] args) {
        example task = new example();
        task.printall();
    }

}

I take it that example task = new example(); should be replaced with Arrayplus1 task = new Arrayplus1();, but you are not giving an argument to the default constructor. So the data, int x and int data[], remain null.

your class code should look like this:

class Arrayplus1 {

    int x;
    int data[];

    // NEW DEFAULT CONSTRUCTOR ADDED
    void example() {
        this.x = 0;
        this.data= new int[0];
    }

    void example(int x) {
        this.x = x+1;
        this.data= new int[x];
    }

    void increaseSizeOfArray(int incrementSize) {
            int copiedArray[] = Arrays.copyOf(data, data.length + incrementSize);
            data = copiedArray;

    }

    void printall() {
        System.out.println(Arrays.toString(data));
    }
}

in the case that you never want your array to be null.

alternatively, you could do this:

public class TaskOne {

    public static void main(String[] args) {
        // INITIALIZED WITH ARGUMENT 0 (rather than no argument)
        example task = new example(0);
        task.printall();
    }

}

once an array is initialized, even when the size is zero, it will not be null, even if it is empty. but if you choose to declare an array reference, and then not initialize it, then it will be null.

Upvotes: 0

Ian2thedv
Ian2thedv

Reputation: 2701

Implement a class that takes an integer array and an int x as its size.

You can accomplish this by the use of a constructor. You can save the arguments passed to the constructor as private variables and reference by using the this notation. (See this)

public class TaskOne {
    private int [] array;
    private int x;
    public TaskOne(int [] array, int x) {
        this.array = array;
        this.x = x;
    }
}

Create a method inside the class that creates a new array whose length is one greater than data’s length

Here you need to simply create a new array. Be sure you set the length of the new array to that of array, plus 1 (to accommodate the extra element, x)

private int [] data = null;

public void createNewArray() {
    this.data = new int[this.array.length + 1];
}

Then create a method to copy all data’s elements into the new array and add the value of x into the last element of the array.

public void populateArray() {
    for (int t = 0; t < this.array.length; t++) {
        data[t] = this.array[t];
    }
    data[array.length] = this.x;
}

Create a method to return all the integers in the new array

This is know as a getter method. You

public int [] getClone() {
    return this.data;
}

You may have noticed that I did not make use of the Arrays.copyOf method. This actually creates and populates the array in one step, so, I kept the two steps separate to conform to the question.

Upvotes: 3

Oğuz K
Oğuz K

Reputation: 400

First of all, you can't create an object of a method. You can create objects of classes. And whenever you have a class, you should have at least one constructor. Also, it seems your methods should return some array and int values. Actually, I don't think this code will compile even. I suggest you to review your book.

Upvotes: 0

Related Questions