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]
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 have a declaration as List<List<byte[]>>, i.e. inner list is list of byte arrays and there are N such lists. I want to identify common byte arrays across all the lists. How would I do that? There is a discussion here, but I donot know if it works for byte arrays
Use the solution you've linked, but as arrays don't have hashCode overriden, you have to wrap them in ByteBuffer, using ByteBuffer.wrap. To extract the array from a ByteBuffer, call array on it.
You can use equals(byte[] a, byte[] a2), as suggested here.
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 3 years ago.
Improve this question
object Test extends App {
val i: Iterable[(String, Long)] = List(("a", 1), ("b", 2))
val sortedMap: SortedMap[String, Long] = i.toList.sortBy(_._2)
}
I don't want to convert Iterable to List/Array etc since it's coming form a jdbc query.
You can't do that. SortedMap sorts by keys, not values.
If you want it sorted by value, you gotta use ListMap, and can't avoid converting to List:
ListMap(i.toList.sortBy(-_._2):_*)
There isn't really too much wrong with converting to list, since you are loading the whole thing in memory anyway. This is faster too, than building a tree one element at a time.
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!
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: