about taking an input in vector in Java [closed] - java

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 have tried to take input in Java language through the vector method. I have tried these two different methods and just want to know the difference between:
Vector v = new Vector();
v.addElement(new Integer(10));
vs
v.add(10);

addElement and add are equivalent, although the return types differ.
However in your case the overall behaviour will be different due to your writing new Integer(10) in one case and 1 in the other case.
In the former case, a new Integer is created with the value 10 and a reference to that is pushed onto the container.
In the latter case, 1 will is auto-boxed to an Integer. But its value just happens to be within the range of interned integers (-128 to +127). So no new Integer is actually created, but a reference to one of the interned integers is pushed onto the container.

There is no difference in function betweenaddElement() and add(), except that add() returns a boolean.
From the JavaDoc for addElement():
Adds the specified component to the end of this vector, increasing its size by one. The capacity of this vector is increased if its size becomes greater than its capacity.
This method is identical in functionality to the add(E) method (which is part of the List interface).
In relation to the boolean that is returned by add(), this is defined in the JavaDoc as returning true if the collection has been changed by the method.
Put another way, this returns true if the element was added to the vector, and false otherwise.

Related

Java: How to get the number of items in a ShortBuffer? [closed]

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
}

Counting the repetition of an element of an array java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I need to count the frequency of an element in arrays.
I used the method
Collections.frequency(Arrays.asList(arr),element);
but I get zero all the times
any ideas ?!
If you are ArrayList consists of elements of custom type
example person bean, or employee object.
Make sure you have overridden equals() method and hash() methods
if you have not overridden these methods that Collection method wont work.
You need to give details about "arr" & element. However, I did came across this some time back when I tried to use an array of primitives such as int[], converting them to a List using Arrays.asList()
There is nothing like List of "int". An Integer would work however, Integer arr[] = {1,1,1,1,3,3,4,5,5,5,6};

Is there the matter with the compareTo return value? [closed]

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
While the contract for compareTo method specifies only the sign of return value, why almost the all method implementations return only -1, 0, 1?
... why do almost the all method implementations return only -1, 0, 1?
I can't speak for other programmers, but I typically do this because it is simpler and more convenient to do that in most case. And most important, it is correct.
I imagine that you are thinking along the lines of doing this:
public int compareTo (MyClass other) {
return this.intField - other.intField;
}
Beware. This code is subtly wrong. See this Q&A: Java Integer compareTo() - why use comparison vs. subtraction?
If you have the read the contract, you have read why.
I'll link it anyway: http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html#compareTo(T)
When you compare two values, say a and b. There are only 3 possibility.
a equals to b
a less than b
a greater than b
So then you use compateTo() you need to check above cases only. Then 0 means there are equals. -1 mean a less than b and 1 means a greater than b
If a and b are objects then reference value consider.

Return True if Arrays have same elements [closed]

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
I could use some help comparing two arrays, previously created in another method. They are called enterNumbers, user input array and drawNumbers, a randomly generated array.
My method header should look like this:
public static boolean containSameElements(int[] enterNumbers, int[] drawNumbers)
{
}
The method should compare the two arrays and return true if the numbers are the same, regardless of the order.
Not looking for the answer, just maybe a place to start.
Just sort them before
Arrays.sort(enterNumbers);
Arrays.sort(drawNumbers);
if(Arrays.equals(enterNumbers, drawNumbers)){
System.out.println("both are same");
}
Well, you can either
Create two histograms (using a hash based map/set) to count elements
in each array, and then compare the sets/maps. This solution is O(n)
space and O(n) time on average. Have a look at Map or Set for this. (Either you want Map or Set depends if existence of duplicates is important or not)
Another solution is sort and iterate. This is O(nlogn) time worst
case. Have a look on Arrays.sort() for this solution.
if (drawNumbers.length != enterNumbers.length)
return false;
List<Integer> base = new ArrayList<Integer>();
for (Integer i : drawNumbers)
base.add(i);
for (Integer i : enterNumbers)
base.remove(i);
return base.isEmpty();
This is a very common "problem" which can be solved using different methods. If I understand you correctly, all of the numbers have be inside the both arrays, but they don't have to be at the same indexes?
Then you can just make a while/for loop with (with two counters; one for each array) and check if the number on index 0 in the first array equals any of the numbers in the second array. If it doesn't the while/for-loop is done and the test failed. If it does go on to the next index in the first array. Continue until everything is tested(all numbers in first array versus the second array) or until a number doesn't exist in both arrays. Good luck

How to store a table in Java and retrieve data from it? [closed]

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
The first column will have serial number, the second, third, and fourth will have x-coordinate, y-coordinate and velocity. I might have to retrieve any of the fields, given the serial number. Say I want to get the y-coordinate of serial number 7, or the velocity of serial number 10. One way is to have an inner class with x,y and velocity and map serial numbers to objects of the inner class. But it makes things look complicated and retrieving a particular field value seems complex. Any better solution?
Create a key object which implements hashCode() and equals() correctly. It appears that your serial numbers might be directly usable.
Create a data object with all fields you will need to store, and a getX() and setX() for each field x.
Create a Map<KeyObject,DataObject> and use that with your (key, data) pairs.
Be very careful to remove outdated objects or you will have a memory leak.
What about using Arrays or Map (Collections -> Map ) ?
With arrays: just create one array with the dimensions you need. In your case:
Array[number_of_itens_to_store][3]
// 3 => 0 = id, 1 = x, 2 =y, 3 = speed
With Maps: take a look at http://download.oracle.com/javase/6/docs/api/java/util/Map.html . You can use your ID as the K (key) and an array as the value.
You can take a look at Collections (http://download.oracle.com/javase/tutorial/collections/index.html) and even create your own collection, just following the tutorials.

Categories