Jump to content
  • Advertisement

Archived

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

WoX

[java] Arrays in classes

This topic is 6303 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!