Four ways to set up output in Java

Source: Internet
Author: User

There are four ways to collection output in Java, such as the following:

A) iterator output. This method applies to all subclasses of the collection.

 Public classhello{ Public Static voidMain (string[] args)throwsException {Set<Person> javaprogramers =NewHashset<person>(); Javaprogramers.add (NewPerson ("Aaron", 25)); Javaprogramers.add (NewPerson ("Larry", 32)); Javaprogramers.add (NewPerson ("Jesse", 33)); Javaprogramers.add (NewPerson ("Mike", 65)); Javaprogramers.add (NewPerson ("Jesse", 33)); Iterator<Person> iter =Javaprogramers.iterator ();  while(Iter.hasnext ()) {System.out.println (Iter.next ()); }    }}

Two) the foreach output. This method applies to all subclasses of the collection.

 Public classhello{ Public Static voidMain (string[] args)throwsException {Set<Person> javaprogramers =NewHashset<person>(); Javaprogramers.add (NewPerson ("Aaron", 25)); Javaprogramers.add (NewPerson ("Larry", 32)); Javaprogramers.add (NewPerson ("Jesse", 33)); Javaprogramers.add (NewPerson ("Mike", 65)); Javaprogramers.add (NewPerson ("Jesse", 33));  for(person item:javaprogramers) {System.out.println (item); }    }}

III) Listiterator. Applies only to subclasses of the list interface.

 Public classhello{ Public Static voidMain (string[] args)throwsException {List<Person> javaprogramers =NewArraylist<person>(); Javaprogramers.add (NewPerson ("Aaron", 25)); Javaprogramers.add (NewPerson ("Larry", 32)); Javaprogramers.add (NewPerson ("Jesse", 33)); Javaprogramers.add (NewPerson ("Mike", 65)); Javaprogramers.add (NewPerson ("Jesse", 33)); Listiterator<Person> iter =Javaprogramers.listiterator (); System.out.println ("Positive sequence output:");  while(Iter.hasnext ()) {System.out.println (Iter.next ()); } System.out.println ("Reverse-order output:");  while(Iter.hasprevious ()) {System.out.println (iter.previous ()); }    }}

Four) enumeration output. Applies only to vector classes.

 Public classhello{ Public Static voidMain (string[] args)throwsException {Vector<Person> javaprogramers =NewVector<person>(); Javaprogramers.add (NewPerson ("Aaron", 25)); Javaprogramers.add (NewPerson ("Larry", 32)); Javaprogramers.add (NewPerson ("Jesse", 33)); Javaprogramers.add (NewPerson ("Mike", 65)); Javaprogramers.add (NewPerson ("Jesse", 33)); Enumeration<Person> enumeration =javaprogramers.elements ();  while(Enumeration.hasmoreelements ()) {System.out.println (enumeration.nextelement ()); }    }}

Four ways to set up output in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.