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 8 years ago.
Improve this question
I'm making a method that takes 2 inputs, one is int[][], the other is int. I need to store that int value as the "row" of the int array. For example if I input (int[][] array, int 3493), I need the row to be {3 4 9 3}. This is basically like the 2048 games, as I need to be able to manipulate the array later. I do not know the proper syntax for this, however. Please help.
Thank you.
As An Sample, suppose x = new int[3][4], x[0], x[1], and x[2] are one-dimensional
arrays and each contains four elements, as shown in the figure x.length is 3, and
x[0].length, x[1].length, and x[2].length are 4
As you see you need two for loops to go through the array.
1. for traversing the row one by one
2. when you are in each row traversing each column one by one
for example:
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 2 years ago.
Improve this question
I wonder how to get the number of items in a ShortBuffer.
I want the number of items that are really in the buffer, not the maximum capacity.
Thanks.
Buffer is not a Collection, but a (comparatively thin) wrapper around a primitive array that provides some useful methods for operating on groups of primitive values. Like a primitive array, it always contains values for each valid index.
Therefore the number of items is always equal to its capacity.
It does not keep track of which indices have already been written to since its creation. And as one major use case is to wrap an existing array while still reflecting all changes to the wrapped array, that would not even be possible to implement.
ShortBuffer holds a position that keeps track on where to put elements. You can use it to know how many elements you put in. The number of elements always equals its capacity as others mentioned.
#Test
fun testShortBuffer() {
val shortBuffer = ShortBuffer.allocate(1024)
println(shortBuffer.position()) // position 0
shortBuffer.put(1)
println(shortBuffer.position()) // position 1
shortBuffer.put(shortArrayOf(2, 3, 4))
println(shortBuffer.position()) // position 4
shortBuffer.clear()
println(shortBuffer.position()) // position 0
}
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 2 years ago.
Improve this question
I am attempting to copy all of the elements in an array into another one, however I am not grasping the concept of System.arraycopy(). Yes, I have researched into it and can't grasp the concept.
Parameters of arraycopy,
arraycopy(Object source, int sourceIndex, Object destination, int destinationIndex, int length);
An Example, Lets say we have 2 arrays each with 5 elements.
int arrayOne[] = {10,20,30,40,50}; // 5 Elements Each.
int arrayTwo[] = {2,4,6,8,10};
If we want to replace the 3rd element (2nd Index) of arrayTwo[2] with the 5th Element (4th Index) of arrayOne[4] we do the following.
System.out.println("Before (System.array.copy): "+arrayTwo[2]);
System.arraycopy(arrayOne,4,arrayTwo,2,1);
System.out.println("After (System.array.copy) : "+arrayTwo[2]);
The Output is as follows.
Before System.array.copy: 6
After System.array.copy : 50
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 3 years ago.
Improve this question
If A is an array of type int, what command would you use to put the value 50 in the first position of the array
A[0] = 50;
This will store 50 in the first element of A.
A[0] = 50; This assign first element to 50.
There is a lot of resource about java array online, i think all the basic tutorial would cover about this.
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 4 years ago.
Improve this question
The thing is I need to create an unidimensional array that represents a certain number of objects.
Those objects are organized as shown in the picture.
Link
And I have to be able to tell wich one is conected to.
The number of objects is the only thing given.
Is there any algorithm of some sort to do this?
This sort of organization is often used to implement heaps in arrays: https://www.geeksforgeeks.org/array-representation-of-binary-heap/
You just put the objects into the array in level order (top 1 first, then the 2 from level 2, then the 4 from level 3, etc.).
Assuming 0-based indexing, then, the object in array[i] has children array[2*i+1] and array[2*i+2].
If your array starts at [1], then the object in array[i] has children array[2*i] and array[2*i+1]
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 7 years ago.
Improve this question
I want to store more than one elements in an index. Example I want to store 23,45,67 at index 0, 12 34 56 at index 1, and so on. How can I store?
May a two dimensional array will help?
int[][] yourArray = {{23,45,67},{0,12,34}};
you can access the values like this
yourArray[0][0]; //will be 23
yourArray[0][1]; //will be 45
yourArray[0][2]; //will be 67
yourArray[1][0]; //will be 0
yourArray[1][1]; //will be 12
yourArray[1][2]; //will be 34
Try to build an ArrayList of ArrayLists, or something similar
There are a couple different ways you could try this.
One option is a 2D array, basically an array of arrays, which is discussed in detail here.
The best way I can think of is using something called a Linked List, which is a data structure that does not have a set numerical order, but instead each item of data also has a variable referring to the next item of data. There's lots of information here on StackOverflow on how to implement it. By creating an array of Linked Lists, index 0 would refer to a list, that could contain as many pieces of data as you would like. It's discussed in detail here.
Best of luck!