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
Array<Body> bodies = new Array<Body>(world.getBodyCount());
world.getBodies();
for (Body body : bodies) {
update(body);
}
Okay, first you're allocating an Array and ensuring the backing array will fit world.getBodyCount(). So, if the existing number of bodies in your world is ten, the ArrayList will have enough memory allocated to store ten bodies initially, but all of these entries will be null.
Then you call world.getBodies(); but this has no access to the local variable bodies, it's definitely not static; therefore your Array is still left uninitialized!
The Array is empty when you come to the for loop, so it execues zero times; nothing gets updated. So; the real trick is you're not accessing the bodies contained within the world properly.
Does getBodies() return a List you should be using instead?
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
This is the main method from where I am trying to pass values to the skill class which accepts array. I do not want to give employee e1 all the skills but just s4, how can I do that so my code becomes logical and correct
You have different way to achieve this.
Taking into consideration the piece of code you showed above, the easiest way to do that which doesn't imply to add any additional method to the Employee class, is to create a new array of Skill while invoking setEmployeesSkills on e1, so your code will appear more or less like this:
e1.setEmployeesSkills(new Skill[]{s4});
Hope that is what you are looking for, otherwise please share more info and I will try to help you further.
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 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 2 years ago.
Improve this question
I tried to search the internet but I didn't see it's been asked. So our lecturer told us, we must write our own linked-list from scratch. Inside the linkedlist have head and tail pointing at another when you add item. Suppose if inside I already add a working integer counter. My question is I have 2 option to check if the linkedlist is empty:
check the head is null
check the counter is 0
my question is which is better in term of efficiency? I know the checking is millisecond matter, but I want to know, in theory, which one got better advantage over another? Sorry I'm not taking Operating System, I not know much the theory.
If you're asking which has better performance, they'll be exactly the same. In both cases you're doing a field or property access followed by a numeric comparison against a constant value. (null is just 0 as a memory address.)
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]