Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Why do we need two-dimensional or multi-dimensional array?
If we want a series of continuous elements of the same type, it can be achieved with simple 1-D array. Like int[] a = new int[15625] will allocate space 15625 elements, and
int [][][] b = new int[25][25][25] will also allocate space 15625 elements
Why do we need them if things can be achieved with 1-d?
You don't need them, but doing grid[x][y] is nicer than grid[x + y*width]
Because sometimes data isn't one dimensional.
For example, the classic game breakout. You could organize your bricks into a one dimensional array and each turn process which row they are in, however it makes more sense to make it a two-dimensional array where the first array is the rows, and the second array is the bricks.
i.e:
bricks[0][3] is the fourth brick of the rirst row
While this could still be done with a one dimensional array, there are math applications where multi-dimensional arrays are necessary.
In addition to this it allows you to have arrays of an infinite1. size, whereas arrays are limited to 230
1. mileage may vary
As mentioned in the comments, multi-dimensional arrays in Java are actually nested arrays. Your nested arrays don't all need to be the same size which is one reason you would use them instead of a single array as you have proposed.
The only other way to declare such an array (off the top of my head) is to declare an array of type Object and then put arrays in it but you lose the basic array type safety. You can access the elements in nested arrays without the [][]... syntax, however.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have used java ArrayList when I inserted one element in the list and i have converted the validatable response to array list when I used assert equals it is showing that both are different
like one is [abcd] other is [[abcd]]
Validatable response = given().spec(request).filter(new ErrorLoggingFilter(errorPrintStream)).pathParams("","").when.post(endpoint).then()
the response is of the form ArrayList when I printed that It came of the form [[abcd]]
To my knowledge, these two are different things
["abcd"] this means an array has one string "abcd" element.
[["abcd"]] this means an array has one array ["abcd"] element.
Yes, ["abcd"] and [["abcd"]] are completely different. Let us understand why.
Let us consider an array ["abcd"]. As you can see, it contains only one element i.e. "abcd". So this is an array that contains a single string value. Now for [["abcd"]], the outer array contains another array inside of it and the inner array contains "abcd". Though their ultimate content seem to be same, they are absolutely different. One is a string array (an array that contains a string value) and the other is an array of string arrays.
Absolutely different, a one-dimensional array, a two-dimensional array,in many languages,reference form it, [0] and [0][0]
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Im trying to understand the relationship between array, ArrayList, and List.
Say I wanted to remove duplicates by converting an array into an ArrayList then into a HashSet, then returned to an array.
int[] start = {1,2,3,4,5};
....
....
....
int[] result = .....
How would that work?
Array is a fixed length data structure. It is a continuous block of memory. Let say you have an array at x3000 with a length of 2. An array stay at x3000 and x3001. Remember, this is oversimplifying the concept, as element size and size per memory location will certainly affect the position of array's elements (not always end at x3001).
List is an abstract data type (ADT), the notable difference between list and array is list has no fixed length. There can be more differences but it may vary from case to case.
ArrayList is a ADT list, but implemented using array. It's like when you do problems that require constructing stack, queue, etc using array.
int[] array = {1, 2, 3, 4, 5};
for(int i : array) {
System.out.print(i+", ");
}
System.out.println();
ArrayList myArrayList = new ArrayList();
for(int num : array) {
myArrayList.add(num);
}
System.out.println(myArrayList);
HashSet<Integer> myHashSet = new HashSet<Integer>();
for(int i = 0;i<myArrayList.size();i++) {
myHashSet.add((Integer) myArrayList.get(i));
}
System.out.println(myHashSet);
This is an example of a normal int array converted to an ArrayList converted to a HashSet.
the relationship between array, ArrayList, and List.
An array is created with the [] and {} notation you showed in your question. It's not dynamically allocated.
A List is an interface, which is just a guide to what methods its implementations must respond to.
An ArrayList is an implementation of a List.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am working on a project that has a list of student name and numbers, for example
James Bloggs,1
Paul Jonson,43
Andt Peters,23
Once I have them in an array I then need them sorted.
What is the best way of going about this. Its not the sort Im stuck on its the referencing the names to the numbers. I would have thought if I do a 2 denominational array only one would be sorted.
Any help would be great,
EDIT: I just realized this question was asking about a 2-dimensional array and my answer doesn't directly deal with that. I am skeptical that arrays should be involved at all. Arrays are usually for dealing with primitive data, and maybe if you are coming from a C background you'd think they'd be the natural thing to use. If you really honestly have to use arrays then this probably isn't the way to go.
https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html
public void foo(){
// Use a TreeMap. It will sort keys on insertion.
Map<Integer,String> nameByNumber = new TreeMap<>();
nameByNumber.put(1, "James Boggs");
// etc. put all the entries in however you need to
List<Integer> sortedNumbers = personByNumber.getKeys();
List<String> namesSortedByNumber = personByNumber.getNames();
}
If you need it to be more organized and complex, you can encapsulate the name and number into a Class with a name and number property. Then you'd still use the number as the key, but you'd have the full class as the value. Do this if you need to have more than just a name, like last name, first name, address, etc.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a sorted array of integers (of size n) and would like to insert a new element into the array, is it possible to do so in O(log(n))? I need to keep an insertion and find computational complexity of O(log(n))).
Right now the only idea I have is to do binary search in order to find the desired index for insertion - this would take O(log(n)), but then I would have to create a new array and copy the entire cells which would take O(n).
EDIT:
It was solved by using an AVL Tree instead, that way any new elements added takes O(log(n)) and finding an element takes O(log(n))
"is it possible to do so in log(n)? " - in short no. From my recollections and experience inserting into an arbitrary position in an array is always O(N), almost by definition. If you want faster performance use something like the TreeMap class.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
SHORT STORY :
I need a 8x8 matrix, that can have many solutions (a
well known existing problem)
Solutions as in, it should have only 8 positions which are 1, rest 0.
A good example can be 8 queen problem.(Placing 8 queens in such a way that they don't kill each other)
Queen problem
so we can create a matrix, which will have 64 positions in all,and 1 represents the current queen position, and 0 a blank position.
LONG STORY:
I am creating an algorithm for Steganography which needs a 8x8 matrix(say A) to store the pixels of an image(8 pixels at a time,8 bits each).
Another 8x8 matrix say B, is to be created which contains data such that Only 8 places should have a 1,rest 0.
These bits are then collectively mapped to matrix A, to check and evaluate an 8 bit resultant
ASCII valued string.
So, I can make many combinations like that, say combinations of 8 kings on a 8x8 chessboard so that the don't kill each other.But it's not a well known,or a tricky problem.
Any ideas for creating such a matrix? The idea can be from anywhere, not generally related to a chessboard.
Consider the 8x8 fields as the adjacency matrix of a graph with 8 nodes and 8 edges
Enumerate all these graphs
Find "interesting" properties of these graphs.
Example: "Has a chromatic number of 3"
Example: "Contains at least / at most / exactly A nodes with degree B"
...
Profit
One way to proceed is to start with a unit matrix (i.e. 1 entries only in the main diagonal and 0 entries elsewhere) and to randomly shuffle the rows.
Since you're not going into detail about the problem itself, I guess what you are asking for is a data structure to accommodate the matrix, and not the algorithm to solve the problem.
A valid datastructure would be a two-dimensional array int[8][8] or even boolean[8][8], since you only need two states per field.
If you're asking for a datastructure that verifies the rule of not more or less than eight marked fields, you can implement a wrapper class around such array that contain the logic to validate.
I might have grossly misunderstood you though.