473,395 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Code meaning

In the following code,please explain Iterator it=t.iterator(); and Object o=it.next();.

import java.util.*;
class TreeSetDemo{
public static void main(String args[]){
TreeSet t=new TreeSet();
t.add("hello");
t.add("a");
t.add("hi");
t.add("b");
System.out.println(t);
Iterator it=t.iterator();
while(it.hasNext()){
Object o=it.next();
if(o.equals("hi"))
it.remove();
}
System.out.println(t);
}}
Apr 4 '07 #1
5 3139
ashsa
45
import java.util.*;
class TreeSetDemo{
public static void main(String args[]){
TreeSet t=new TreeSet();
t.add("hello");
t.add("a");
t.add("hi");
t.add("b");
System.out.println(t);
Iterator it=t.iterator(); // iterator() method in TreeSet class returns an Iterator
while(it.hasNext()){ // instance which can be used to iterate through all the
Object o=it.next(); // elements of the TreeSet. next() method of the Iterator
if(o.equals("hi")) // class returns the next element in the TreeSet as an
it.remove(); // Object. Important to note is that the iterator initially point
} // to the position -1 th position of the tree. so hasNext()
System.out.println(t);// will return true if there is an element in the zeroth place.
}}[/quote]

Hope thats clear !! Na ?
Apr 4 '07 #2
r035198x
13,262 8TB
In the following code,please explain Iterator it=t.iterator(); and Object o=it.next();.

import java.util.*;
class TreeSetDemo{
public static void main(String args[]){
TreeSet t=new TreeSet();
t.add("hello");
t.add("a");
t.add("hi");
t.add("b");
System.out.println(t);
Iterator it=t.iterator();
while(it.hasNext()){
Object o=it.next();
if(o.equals("hi"))
it.remove();
}
System.out.println(t);
}}
1.) Use code tags when posting code.
2.)What do you think the lines mean first.
Apr 4 '07 #3
hirak1984
316 100+
iterator iterates over objects in the List.Go through any standard book on java, in the collections chapter you will learn more about iterator.

Btw are you sure this code runs?

In the following code,please explain Iterator it=t.iterator(); and Object o=it.next();.

import java.util.*;
class TreeSetDemo{
public static void main(String args[]){
TreeSet t=new TreeSet();
t.add("hello");
t.add("a");
t.add("hi");
t.add("b");
System.out.println(t);
Iterator it=t.iterator();
while(it.hasNext()){
Object o=it.next();
if(o.equals("hi"))
it.remove();
}
System.out.println(t);
}}
Apr 4 '07 #4
Thanks a lot. I have one doubt.
t.add("hello");
t.add("a");
In this is element object ?.How is this possible without new key word.Pls reply me.


import java.util.*;
class TreeSetDemo{
public static void main(String args[]){
TreeSet t=new TreeSet();
t.add("hello");
t.add("a");
t.add("hi");
t.add("b");
System.out.println(t);
Iterator it=t.iterator(); // iterator() method in TreeSet class returns an Iterator
while(it.hasNext()){ // instance which can be used to iterate through all the
Object o=it.next(); // elements of the TreeSet. next() method of the Iterator
if(o.equals("hi")) // class returns the next element in the TreeSet as an
it.remove(); // Object. Important to note is that the iterator initially point
} // to the position -1 th position of the tree. so hasNext()
System.out.println(t);// will return true if there is an element in the zeroth place.
}}
Hope thats clear !! Na ?[/quote]
Apr 5 '07 #5
hirak1984
316 100+
You need not need new keywird for that...
But I cud have helped you more precisely if you would have included the class Treeset in your code.
Can you?


Thanks a lot. I have one doubt.
t.add("hello");
t.add("a");
In this is element object ?.How is this possible without new key word.Pls reply me.




Hope thats clear !! Na ?
[/quote]
Apr 6 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

27
by: Fuli Chang | last post by:
I don't like other people see my html code. Is there a way to hide it? Thanks.
4
by: yxq | last post by:
Hello, There are the C# codes£¬but i know a little about C#, i want to convert to VB.NET, i have try the url(http://authors.aspalliance.com/aldotnet/examples/translate.aspx), but the converted...
0
by: DavidL | last post by:
Error Code meaning in Winscard.dll functions? I am using the SCardTransmit function from winscard and getting errors back like: 0x00000006 0x0000054f 0x0000045d I can not find the...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
2
by: SAIRAAM | last post by:
hi everyone..... i am working on a performance testing tool for which i had been given a sample code of the existing tool for which the user interface is designed in vb... perl plugin programs are...
4
by: Mohitz | last post by:
Any pointers as to how one should go about porting procedural type code into object oriented code?? --- Mohit
78
by: Jeremy J Starcher | last post by:
(Request for Discussion) I've put together a guide that I hope will help novice coders avoid the same hair pulling that I went through. I'm open for comments about it. Have I missed the...
14
by: Hendrik Maryns | last post by:
Hi, Since I was so pleased to discover how to enable my keyboard to type symbols like … directly, I made a howto-page about it: http://tcl.sfs.uni-tuebingen.de/~hendrik/keyboard.shtml. I...
3
pradeepjain
by: pradeepjain | last post by:
$other->setDecorators(array( 'Description' , 'Label' , array( array('divLabel' => 'HtmlTag') , array('tag' => 'div' , 'class' => 'fieldItemLabel')...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.