public class Test {
public static void main(String[] args) {
HashSet s = new HashSet();
for(short i = 0; i<100;i++){
s.add(i);
s.remove(i-1);
}
System.out.println(s.size());
}
}
< spoiler removed >
13 4682
If you really can't find the answer by thinking about it simpy cook up a compilable
and runnable piece of code, run it and see for yourself.
kind regards,
Jos
ps. answer: 1
If you really can't find the answer by thinking about it simpy cook up a compilable
and runnable piece of code, run it and see for yourself.
kind regards,
Jos
ps. answer: 1
Whether it is possible to add the integer to hashset
Whether it is possible to add the integer to hashset
Since Java 1.5 it is: autoboxing (but then you need a Set<Short>)
kind regards,
Jos
Since Java 1.5 it is: autoboxing (but then you need a Set<Short>)
kind regards,
Jos
- public class Test {
-
public static void main(String[] args) {
-
HashSet s = new HashSet();
-
for(short i = 0; i<100;i++){
-
s.add(String.valueOf(i));
-
s.remove(String.valueOf(i-1));
-
}
-
System.out.println(s.size());
-
}
-
}
i just run above code like this so wat iam asking
i just run above code like this so wat iam asking
I'm sorry I don't understand what you're saying; the original example uses a raw
Set; since Java 1.5 you shouldn't do that anymore; use a Set<Short> or Set<Integer>
instead. Is that what you're talking about?
kind regards,
Jos
OK thanks ,nothing i dont know what is mean <set>Short.
OK thanks ,nothing i dont know what is mean <set>Short.
Read this article for a nice tutorial on generics.
kind regards,
Jos
I don't think you run your code successfully.
You should get an error for calling String.valueOf(-1), (Index out of bounds exception) because the first time inside the loop i=0 ! -
...
-
for(short i = 0; i<100;i++){
-
...
-
s.remove(String.valueOf(i-1));
-
}
-
...
-
i just run above code like this so wat iam asking
I don't think you run your code successfully.
You should get an error for calling String.valueOf(-1), (Index out of bounds exception) because the first time inside the loop i=0 !
The value -1 is a value to be deleted from a Set, not its index.
kind regards,
Jos
Thanks Josh may know wre r u from
I have found the answer to this question at
http://java4all.info/corejava/interview-questions/java-interview-questions.html#q4
Thanks
I have found the answer to this question at
http://java4all.info/corejava/interview-questions/java-interview-questions.html#q4
Thanks
Bloody autoboxing ... again it got me ...
kind regards,
Jos
out put : 100.
-----------------------------------------------------------------
remove() in HashSet
public boolean remove(Object o)
Removes the specified element from this set if it is present.
Specified by:
remove in interface Set
Overrides:
remove in class AbstractCollection
Parameters:
o - object to be removed from this set, if present.
Returns:
true if the set contained the specified element.
------------------------------------------------------------------------------------
Since you are requesting size ,you will get 100.
---------------------------------------------------------------------------
JDK1.5 uses concept called autoboxing.
You face no problem in executing the program.
--------------------------------------------------------------------
regards,
shailesh
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Zulik |
last post by:
Hello,
I have a problem with MessageDigest MD5 in Java. I want to calculate the
digest from a file, encode it in base64 and display it. Now, according to
MD5 spec, digest shall be 128 bits so,...
|
by: Xah Lee |
last post by:
What are OOP's Jargons and Complexities
Xah Lee, 20050128
The Rise of Classes, Methods, Objects
In computer languages, often a function definition looks like this:
subroutine f (x1, x2, ...)...
|
by: jay |
last post by:
I was told that an ostringstream should be terminated by ends.
However, the following code snippet does not do what I would expect if
that were the case:
#include <iostream>
#include...
|
by: Neil Zanella |
last post by:
OK, this time the compiler's got me a little bit puzzled, simply because it is
doing something I am not expecting. My understanding, according to the
documentation of std::vector<>::resize(), is...
|
by: typingcat |
last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so
on. I've tried many PHP IDEs today, but almost non of them supported
Unicode (UTF-8) file.
I've found that the only Unicode...
|
by: Thomas G. Marshall |
last post by:
Arthur J. O'Dwyer <ajo@nospam.andrew.cmu.edu> coughed up the following:
> On Thu, 1 Jul 2004, Thomas G. Marshall wrote:
>>
>> Aside: I've looked repeatedly in google and for some reason cannot
>>...
|
by: Claire |
last post by:
I've explained the problem in the following code snippet. I want to share
single functions for use by several data types.
In this function I call different functions to fill out the original object...
|
by: Carlitos |
last post by:
Hi there,
What would be the counterpart for Java's StringTokenizer class in .NET?
In fact, does anybody know a good web site that gives this kind of
information, like a Java-.NET/.NET-Java...
|
by: crabsdf |
last post by:
My project is a single method class which takes a xml object and, using Apache FOP, transforms it into a PDF which is returned as an output stream.
Here is full code
package...
|
by: Marc |
last post by:
Comming from JSP-HTML when I hear 'script' I think of JavaScript or perhaps
VBScript. But I think in ASP.NET they mean something else? I am bit confused
now.
Reading a book I see the following:...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |