Archived

This topic is now archived and is closed to further replies.

WoX

[java] Arrays in classes

Recommended Posts

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!!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
  class ClassWithArray
{
public:
int a_grid[1][1][1][1];

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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites