[Java class set] _ foreach and enumeration interface notes
Objectives of this chapter:
Understanding foreach's support for set outputUnderstanding the enumeration interface and usage requirements
3. Details
Foreach can not only output arrays, but also output class sets.
import java.util.ArrayList;import java.util.List;public class ForeachDemo01{ public static
Com.haixu.collection;import Java.util.collection;import Java.util.hashset;import java.util.Iterator;/* * Iterator used to traverse the collection * */public class Iteratortest {public static void main (string[] args) { collectionThe Foreach loop iterates over the elements in the collection collection and the home is a little more concise!The Foreach loop iteration Azimuth collection element is that the
In general, access to data in List,array,set is accessed in the following wayint [] Nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, ten }; for (int i = 0; i ) { System.out.println ("i =" + i);}Since Java1.5, there has been a more convenient way to access: foreachint [] Nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, ten }; for (int x:nums) { System.out.println ("Value is:" + x); + = x;}
Grammar:
For (type variable name: Collection variable name) {statements;}
Precautions
The use of Java collection classes can be said to be ubiquitous, the total we can be divided into three chunks, respectively, from the collection interface extension of the list, set and the form of key-value pairs to store the map type collection. Many situations require that we iterate through the elements in the collection and do the appropriate processing. The following is a summary of the traversal of various types of collections, with regard to
Here for is a foreach in Java, used to iterate over the array.for (int i:d) is the data that iterates through the array d of the int type every time you access it, put it in the int type I.and for (int i=0;ipublic class E1 {public static void Main (string[] args) {Int[]d=new int[] {1,2,3,4,64,1234,3124,657,22};System.out.println ("d.length=" +d.length);for (int i:d) { System.out.printl
) - { the System.out.println (s); * } $ Panax Notoginseng - int[] arr = {3,1,5,7,4}; the for(intI:arr) + { A System.out.println (i); the } + - $ /* $ * Can I use the advanced for Traversal map collection? cannot be used directly, but you can turn the map into a single-column set, you can use the - */ - themapNewHashmap(); -Map.put (3, "Zahngsan");WuyiMap.put (at "Zahngsan"); theMap.put (
The first format is: for (element Type element variable: traversal Object) { executed code}For example:public class Test {public static void main (string[] Args) { //define an integer array, save score Information int[] scores = {89, 72, 64, 58, 93 }; Sorts the array of arrays classes Arrays.sort (scores); Use foreach to iterate through the elements in the output array for (int sco:scores) {System.out.println (sco);}}}He
(String name) { This. Name =name; } Public voidsay () {System.out.println ("I ' m" +name); }} Public classMain { Public Static voidMain (string[] args) {/*** foreach Syntax * for (type variable name: Array or collection) {//output operation}*/string[] Names= {"1", "2", "3", "4"}; for(String i:names) System.out.println (i); /*** Variable parameter of method * 1. Only one * 2. When there are multiple parameters, the variable parameter is placed in
of the stack -: IF_ICMPLT - //Compare the size of the value of the stack top two int, when the result is small at 0 o'clock Jump -:return //returns void from the current method}We only need to pay attention to the 6,16~23 line, we can find that the code is to take the value of the array reference copy in the top of the stack, and then take the variable from the top of the stack in the local variable 3, then put 55 on the top of the stack, and then 55 out of the local
top of the stack -: IF_ICMPLT - //Comparison of the size of the stack top two int type, when the result is small at 0 o'clock Jump -:return //returns void from the current method}We just need to focus on the 6,16~23 line. It is possible to find that the code is to take the value out of the array reference copy and place it on top of the stack, and then remove the variable from the top of the stack in local variable 3. Then put 55 on the top of the stack, and then put
public static void Main (string[] args) {
listException in thread "main" java.lang.NullPointerException
At Com.joyplus.util.Tets06.main (tets06.java:14)
This test proves not to judge Null, there will be abnormal situation, for a long time mistakenly thought that do not need to judge, this is the wrong idea, in this
public static void Main (string[] args) {
list
Exception in thread "main" java.lang.NullPointerException
At Com.joyplus.u
, you cannot call the Remove method inside the iterator to delete the element3. the difference between the normal for loop and the enhanced for loop: The normal for loop can have no variable target, and the enhanced for loop must have a variable target5. Example1 Package com.dhb.pattern;2 3 import java.util.HashSet;4 /**5 * @author dshore/2018-6-86 *7 */8 Public classDemo3 {9 Public Static voidMain (string[] args) {Ten //Collection OneHashsetSet=NewHashset(); A
Java-implemented fuzzy match of files in a folder and delete function example, java example
This example describes how to use Java to perform fuzzy match and delete a file in a folder. We will share this with you for your referenc
Java Implementation of the method example for solving the n-degree polynomial, java polynomial example
The example in this article describes how Java implements the solution to a polynomial of n times. We will share this with you
How to define an abstract attribute example in java and how to define an example in java
Preface
This article mainly introduces the definition of an abstract attribute in java and shares it for your reference. I will not talk about it much below. Let's take a look at the det
Java programming example code based on three algorithm questions of quick sorting, java example
Overview
Quick sorting is an update of the Bubble sorting we have learned before. They all belong to the exchange sorting class and are sorted by continuous comparison and movement. Quick sorting is a very efficient sorting
Java Web implementation method example for adding scheduled tasks, Java Web implementation example
This example describes how to add scheduled tasks in Java Web. We will share this with you for your reference. The details are as f
Java-implemented n-order Curve Fitting Function example, java example
This example describes the n-order Curve Fitting Function implemented by Java. We will share this with you for your reference. The details are as follows:
In th
JAVA super simple crawler example (1), java crawler example
Crawls the data of the entire page and effectively extracts information. comments are not nonsense:
Public class Reptile {public static void main (String [] args) {String url1 = ""; // input the page address you want to crawl. InputStream is = null; // create
Java basics-an example of access permissions, a java permission example
1. Review access Modifiers
= Public: the class, attribute, and method modified by it. It can be accessed not only across classes, but also across packages.
= Private: you can modify data members, constructor methods, methods, and classes. The modif
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.