Creating an Array of Objects in Java

Creating an array of objects in Java

This is correct.

A[] a = new A[4];

...creates 4 A references, similar to doing this:

A a1;
A a2;
A a3;
A a4;

Now you couldn't do a1.someMethod() without allocating a1 like this:

a1 = new A();

Similarly, with the array you need to do this:

a[0] = new A();

...before using it.

How can I create an array of object and takes user input in java? I have wrote the below code but confusing me

ArrayOfObject[] arr = new ArrayOfObject[5];

In this step you just initialize your Array NOT your object inside it.

So in each step, you must init each of your object by doing this:

for (int i = 0; i < arr.length; i++) {    
arr[i] = new ArrayOfObject();
System.out.println("Enter First Name");
...

Java : creating an array of array of objects

This line of your code (in method createMap())...

Field[][] map = new Field[width][length];

creates a two dimensional array but every element in the array is null.

Hence this line of your code (also in method createMap())

map[i][j].setX(j);

will throw a NullPointerException.

You need to explicitly create Field objects.

Also the Y coordinate of some of the Field elements in the map is zero as well as the X coordinate in some of the elements because (also in method createMap()) you start the for loops with zero. In order to fix that I add one to i and j when I call setX() and setY().

Here is the corrected code for the for loops in method createMap()

for( int i = 0 ; i < width ; i++ ){
for( int j = 0 ; j < length ; j++ ){
map[i][j] = new Field();
map[i][j].setX(j + 1);
map[i][j].setY(i + 1);
}
}

The only thing left to do is call method createMap(). Since map is a member of class CoordinateSystem, it seems logical to call createMap() from the constructor of CoordinateSystem.

public CoordinateSystem(int width, int length) throws MyException {
this.width = width;
this.length = length;
map = createMap(width, length);
}

Finally, for the sake of completeness, here is the entire [corrected] code of class CoordinateSystem

public class CoordinateSystem {

private int length;
private int width;
private Field[][] map;

public CoordinateSystem(int width, int length) throws MyException {
this.width = width;
this.length = length;
map = createMap(width, length);
}

public int getLength() {
return this.length;
}

public int getWidth() {
return this.width;
}

public class Field {
private int x;
private int y;

public void setX(int x) {
this.x = x;
}

public int getX() {
return x;
}

public void setY(int y) {
this.y = y;
}

public int getY() {
return y;
}
}

public Field[][] getMap() {
return map;
}

// Initializing a coordinate to each "field"
public Field[][] createMap(int width, int length) throws MyException {
if(width > 0 && length > 0){
Field[][] map = new Field[width][length];
for( int i = 0 ; i < width ; i++ ){
for( int j = 0 ; j < length ; j++ ){
map[i][j] = new Field();
map[i][j].setX(j + 1);
map[i][j].setY(i + 1);
}
}
return map;
}
else{
throw new Exception("Sorry, can't create a field of width or height = 0 ");
}
}

public static void main(String[] args) throws MyException {
CoordinateSystem board = new CoordinateSystem(8, 9);
for( int i = 0 ; i < 8 ; i++ ) {
for( int j = 0 ; j < 9 ; j++ ) {
System.out.print(board.getMap()[i][j].getX());
System.out.println(board.getMap()[i][j].getY());
}
}
}
}

error in create array object in java code

It's happening because you just initialize an array, but not initialized the elements of the array. If you print the elements of the array to System.out, you will see that all of them are null. You have to initialize each element of the array.

card[] c = new card[3];
c[1] = new card();
c[1].setword("Hello");
c[2] = new card();
...

Objects in array in Java

There is a difference between declaration and initialization.

Dog[] pets declares that the variable pets is an array of Dogs
pets = new Dog[7] initializes the variable pets, it assigns it a value. The value is an array of size 7 full of null references.

It is the same for primitives :

int i; //declaration
i = 5; //initialization

As well as you can write

int i = 5;

you can write

Dog[] pets = new Dog[7];

In this case, you do the declaration and initialization on the same line.

Creating an array of Objects from reading a .txt file and saving line by line into object array

Please use this code

   public Record[] readRecord(String pathFile) throws IOException {
List<String> list = Files.readAllLines(Paths.get(pathFile));
Record[] records = new Record[list.size()];
for(int i = 0; i< list.size(); i++) {
records[i] = new Record(list.get(i));
}
return records;
}


Related Topics



Leave a reply



Submit