I have been looking through the Java Tutorials at the Interfaces tutorial, specifically on Collections (Set, List, Queue, etc.) and I came across the fact that a Set cannot contain duplicates in its elements.
My problem is the fact that I do not fully understand how to create a set of a multi-dimensional array of an unknown size.
In order to fill the multi-dimensional array, I will be placing 1’s and 0’s inside of an array so that each one will look like the following: (if it fits the criteria I am looking for)
[ 0 1 1 0
0 1 1 0
0 1 0 0
0 1 0 0
0 0 0 0 ]
Or something of that nature. I would like to think this can be accomplished through declaring an multi-dimensional array like:
int[][] array = new int[5][];
Yet I cannot understand how that would work with filling multiple array elements or how to accomplish this with a set.
Please let me know if this is not clear enough.
List’s can contain duplicates, sets cannot. You can declare a (dynamic) multidimensional structure in several ways, heres one:
And so on and so forth. To access the elements of the list utilize the
getmethod in a way similar to what you would do with arrays:Having said that, you only need to use this nested
Listsetup if your multidimensional structure needs to be 100% dynamic, aka you need the ability to grow the rows and columns constantly throughout the execution of your logic. You can actually use an ordinary array for your multidimensional structure, assuming that the number of rows can be determined ahead of time, and that each row’s length will not change after that row has been initialized. Case in point:And you access the elements with your usual array semantics (
multiDimensional[0][3]).