Advertisement

[java] Arrays in classes

Started by June 23, 2001 01:11 PM
1 comment, last by WoX 23 years, 7 months ago
How can you put an array as a class member? When I try, the array doesn''t seem to be initialized public class Classwitharray { int a_grid[][][][] = new int[1][1][1][1]; public Classwitharray() { a_grid[0][0][0][0] = 1; } }; OR public class Classwitharray { int[][][][] a_grid; public Classwitharray() { a_grid = new int[1][1][1][1] a_grid[0][0][0][0] = 1; } }; or maybe something else please help!!
  class ClassWithArray{public:	int a_grid[1][1][1][1]; 	ClassWithArray()	{		a_grid[0][0][0][0] = 1;	}};  
Advertisement
do this:

public class ClassWithArray
{

int[][][][] a_grid = new int[1][1][1][1];

public ClassWithArray()
{
a_grid[0][0][0][0] = 1;

}

}

OR

public class ClassWithArray
{

int[][][][] a_grid;

public ClassWithArray()
{
a_grid = new int[1][1][1][1];
a_grid[0][0][0][0]=1;
}

}

note you don''t need a semicolon after the braces. Basically the key point is that you should declare a variable of type int[][][][], not the messed up C way where it looks like you are declaring an int. Both of the above versions work perfectly and are essentially indistinguisable from each other.

This topic is closed to new replies.

Advertisement