0

I want to store an array of 100 int elements in rows ie 1 row of 100 int datatypes.And every single element contains an array of 100 objects.How to do this in java or android.

2
  • 1
    Your question is ambiguous and self-contradictory. On the one hand, you say that the elements have type int. On the other hand, you say that each element contains an array of 100 objects. It is impossible for an int to contain anything. Please try to express yourself more clearly. Commented Dec 21, 2010 at 7:02
  • Sorry ,Actually I want to store 100 Objects in every column in 200x100 matrix Commented Dec 21, 2010 at 7:37

4 Answers 4

6

Use a collection, a "List of List":

List<List<YourType>> matrix2D = new ArrayList<List<YourType>>();

This structure can easily store a table with 200 rows and 100 columns where each element is of type YourType.

Otherwise - if your size is fixed and you just want to store YourType values, theres no need for generics:

int rows = 200;
int columns = 200;
YourType[][] matrix2D = new YourType[rows][columns];
Sign up to request clarification or add additional context in comments.

Comments

0

Here is how you can initialize and fill a two-dimensional Object array:

    Object value = 42;
    int rows = 100;
    int columns = 100;
    Object[][] myData = new Object[rows][columns];
    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < columns; c++) {
            myData[r][c] = value;
        }
    }

Note that the primitive int value 42 is autoboxed when it is stored in the Object[][]. If you don't want this you could use an int[][] instead.

Comments

0

If your type is int, it which be much more compact than using Integer type. (up to 6x smaller) To create a fixed size array you can

int[][] matrix = new int[200][100];
// set all values to 42.
for(int[] row : matrix) Arrays.fill(row, 42);

Comments

-1

I think you need this.

100 T(Your Object) Objects in 200x100 matrix(array)

T[][][] t = new T[200][100][100] ; 

t[0][0][1] = new T();
t[0][0][2] = new T();
...
t[0][0][99] = new T();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.