Turn: Java for loop several uses Detailed

Source: https: //www.cnblogs.com/hellochennan/p/5373186.html

Source: https: //www.cnblogs.com/hellochennan/p/5373186.html

This paper is ideal for beginners of Java programmers , mainly to look at several of the for loop uses Java, very detailed analysis, take a look.

J2SE 1.5 provides another form of for loop. With this form of the for loop, can be used more simply stepping through an array of objects, and other types of Collection. This article describes the use of specific way this cycle, illustrating how this traversal can define their own class, and explain some of the common problems and the mechanism.

In a Java program, to be "processed one by one" - or "walk" - the elements of an array or a Collection of time, generally use a for loop to achieve (of course, with other types of circulation is not no Yes, just do not know because for the word length is relatively short, or because the meaning of the word and for comparison with this operation, for circulating much more common than other circulating at this time).

For through the array, this cycle generally take such wording:

Listing 1: The traditional way to iterate

 / * Create an array * /
 int[] integers = {1, 2, 3, 4};
 / * Start traversing * /
 for (int j = 0; j < integers.length; j++) {
     int i = integers[j];
     System.out.println(i);
 }

For traversing Collection object, this cycle is typically take the form:

Listing 2: The traditional way to traverse the Collection object

/ * Create a Collection * /
 String[] strings = {"A", "B", "C", "D"};
 Collection stringList = java.util.Arrays.asList(strings);
 / * Start traversing * /
 for (Iterator itr = stringList.iterator(); itr.hasNext();) {
     Object str = itr.next();
     System.out.println(str);
 }

In the latest version of the Java language --J2SE 1.5, the introduction of another form of the for loop. With this form for the cycle, now in a more simple way to traverse the work.

 1, the second for loop

Not strictly speaking, Java is the second basic cycle for this format:

 for (loop loop variable type variable name: the object to be traversed) loop

With this grammar, traversing operation can take an array of such wording:

Listing 3: Simple way to iterate

 / * Create an array * /
 int[] integers = {1, 2, 3, 4};

 / * Start traversing * /
 for (int i : integers) {
     System.out.println (i); / * sequentially outputs "1", "2", "3", "4" * /
 }

As used herein for the cycle, would be seen during compilation is of the form:

Equivalent code is a simple way to iterate: Listing 4

/ * Create an array * /
 int[] integers = {1, 2, 3, 4};

 / * Start traversing * /
 for (int variable name A = 0; A variable name <integers.length; variable name A ++) {
     System.out.println (integers [A variable name]); / * sequentially outputs "1", "2", "3", "4" * /
 }

Here, "A variable name" is a confusion of names by the compiler will not automatically generated.

Traverses a Collection of operation also may be employed such wording:

Listing 5: a simple way to traverse the Collection

 / * Create a Collection * /
 String[] strings = {"A", "B", "C", "D"};
 Collection list = java.util.Arrays.asList(strings);

 / * Start traversing * /
 for (Object str : list) {
     System.out.println (str); / * sequentially outputs "A", "B", "C", "D" * /
 }

As used herein for the cycle, it will be seen during the compilation is of the form:

Equivalent code is a simple way of traversing the Collection: 6 Listing

 / * Create a Collection * /
 String[] strings = {"A", "B", "C", "D"};
 Collection stringList = java.util.Arrays.asList(strings);

 / * Start traversing * /
 for (Iterator variable name B = list.iterator (); variable name B .hasNext ();) {
     Object str = variable name .next B ();
     System.out.println (str); / * sequentially outputs "A", "B", "C", "D" * /
 }

Here the "variable name B" is also a confusion of names by the compiler will not automatically generated.

Because during compilation, J2SE 1.5 compiler will loop for this form, as a form corresponding to the traditional, there is no fear of performance problems arise.

Not "foreach" and "in" reasons

Java uses "for" (rather than the more specific sense of "foreach") to direct this general called "foreach loop" recycling, and use the ":" (rather than the more specific meaning "in") to split the name and the object to be traversed loop variable. The main reason for this is to avoid the introduction of new keywords, causing compatibility issues - in the Java language, is not allowed to use keywords as variable names, although the case of using the "foreach" This name not very much, but "in" is a name often used to represent the input stream (for example java.lang.System class, there is a name called "in" a static property, which means "the standard input stream").

Indeed through clever design syntax, so only the keywords have special meaning in a particular context, to allow them to be used as a common identifier. However, this would complicate the syntax of strategy, it has not been widely adopted.

 "For-each loop" of history

"For-each loop" is not a control structure only recently. In 1979 the official release of the Bourne shell (the first of a mature UNIX command interpreter) has been included in this control structure (circulation "for" and "in" to guide, with the body of the loop "do" and "done "to identify).

2, to prevent the loop variable modification within the loop body

By default, the compiler is to allow for circulation of the second loop, the loop variable re-assignment. However, because this approach had no effect on the situation outside the loop, but also likely to cause difficulties in understanding the code, it is generally not recommended.

Java provides a mechanism to put the ban such operations during compilation. Specific method, in the previous cycle plus a variable type "final" modifier. Thus, the assignment of the loop variable in the loop body, it will lead to a compiler error. With this mechanism, you can effectively eliminate intentionally or unintentionally be "modified loop variable in the loop body" of the operation.

Listing 7: Prohibition reassigned

 int[] integers = {1, 2, 3, 4};
 for (final int i : integers) {
     i = i / 2; / * compile-time error * /
 }

Note that this only prohibits the loop variable re-assignment. Assigns them the loop variable, or call to make changes in the content of the loop variable method, is not prohibited.

Listing 8: allows you to modify the state

Random[] randoms = new Random[]{new Random(1), new Random(2), new Random(3)};
 for (final Random r : randoms) {
     r.setSeed (4); / * all Random object set to use the same seed * /
     System.out.println (r.nextLong ()); / * same seed, the first result is the same * /
 }

3. Type compatibility problems

To ensure the loop variable energy, have been security assignment at the time of the beginning of each cycle, J2SE 1.5 has some restrictions on the type of the loop variable. Under these restrictions, the type of the loop variable can have a number of options:

Variable cycle type may be the type of element and the object to be traversed in the same. For example, an int type array to traverse a loop variable int [] type with an Object to traverse a loop variable Collection like.

Listing 9: the same type of use and to be traversed loop variable elements in the array

int[] integers = {1, 2, 3, 4};
 for (int i : integers) {
     System.out.println (i); / * sequentially outputs "1", "2", "3", "4" * /
 }

Listing 10: the same type of use and to be traversed loop variable elements in Collection

Collection< String> strings = new ArrayList< String>();
strings.add("A");
strings.add("B");
strings.add("C");
strings.add("D");
for (String str : integers) {
System.out.println (str); / * sequentially outputs "A", "B", "C", "D" * /
}

Variable cycle type may be superior to the element type of an object to be traversed in. For example, a variable of type int loop through the array to a byte [] type, with variable loop type Object to traverse a Collection <String> (String all the elements are Collection) and the like.

Listing 11: Using superior type of element objects to be traversed in a loop variable

String[] strings = {"A", "B", "C", "D"};
Collection< String> list = java.util.Arrays.asList(strings);
for (Object str : list) {
System.out.println (str); / * sequentially outputs "A", "B", "C", "D" * /
}

Type variable cycle can be automatically converted there is a relationship between the type element and the object to be traversed in. J2SE 1.5 contains a mechanism "Autoboxing / Auto-Unboxing", allowing the compiler when necessary, automatically convert between types and their basic parcel (Wrapper Classes). Thus, with the type Integer a loop through the array variable int [] type, or a variable byte type of loop to traverse a Collection <Byte>, it is also possible.

Listing 12: energy and type of objects to be traversed automatic conversion elements of the loop variable of type

int[] integers = {1, 2, 3, 4};
for (Integer i : integers) {
System.out.println (i); / * sequentially outputs "1", "2", "3", "4" * /
}

Note that the "element type" here said, is to be traversed by the object decision - if it is an array [] type Object, then the type element is Object, even if there is a String object is loaded in this way.

Collection element types can be defined

Up until J2SE 1.4, the type that can be saved in the Collection object has failed to define a Java program - all of them are seen as the most generic Object object. Until after the J2SE 1.5, the introduction of the "generics (Generics)" mechanism, the problem was solved. Can now Collection <T> to represent T Collection of all types are element.

Accumulate, actions speak louder than words @ fowls cast forest

Source: https: //www.cnblogs.com/hellochennan/p/5373186.html

Source: https: //www.cnblogs.com/hellochennan/p/5373186.html

This paper is ideal for beginners of Java programmers , mainly to look at several of the for loop uses Java, very detailed analysis, take a look.

J2SE 1.5 provides another form of for loop. With this form of the for loop, can be used more simply stepping through an array of objects, and other types of Collection. This article describes the use of specific way this cycle, illustrating how this traversal can define their own class, and explain some of the common problems and the mechanism.

In a Java program, to be "processed one by one" - or "walk" - the elements of an array or a Collection of time, generally use a for loop to achieve (of course, with other types of circulation is not no Yes, just do not know because for the word length is relatively short, or because the meaning of the word and for comparison with this operation, for circulating much more common than other circulating at this time).

For through the array, this cycle generally take such wording:

Listing 1: The traditional way to iterate

 / * Create an array * /
 int[] integers = {1, 2, 3, 4};
 / * Start traversing * /
 for (int j = 0; j < integers.length; j++) {
     int i = integers[j];
     System.out.println(i);
 }

For traversing Collection object, this cycle is typically take the form:

Listing 2: The traditional way to traverse the Collection object

/ * Create a Collection * /
 String[] strings = {"A", "B", "C", "D"};
 Collection stringList = java.util.Arrays.asList(strings);
 / * Start traversing * /
 for (Iterator itr = stringList.iterator(); itr.hasNext();) {
     Object str = itr.next();
     System.out.println(str);
 }

In the latest version of the Java language --J2SE 1.5, the introduction of another form of the for loop. With this form for the cycle, now in a more simple way to traverse the work.

 1, the second for loop

Not strictly speaking, Java is the second basic cycle for this format:

 for (loop loop variable type variable name: the object to be traversed) loop

With this grammar, traversing operation can take an array of such wording:

Listing 3: Simple way to iterate

 / * Create an array * /
 int[] integers = {1, 2, 3, 4};

 / * Start traversing * /
 for (int i : integers) {
     System.out.println (i); / * sequentially outputs "1", "2", "3", "4" * /
 }

As used herein for the cycle, would be seen during compilation is of the form:

Equivalent code is a simple way to iterate: Listing 4

/ * Create an array * /
 int[] integers = {1, 2, 3, 4};

 / * Start traversing * /
 for (int variable name A = 0; A variable name <integers.length; variable name A ++) {
     System.out.println (integers [A variable name]); / * sequentially outputs "1", "2", "3", "4" * /
 }

Here, "A variable name" is a confusion of names by the compiler will not automatically generated.

Traverses a Collection of operation also may be employed such wording:

Listing 5: a simple way to traverse the Collection

 / * Create a Collection * /
 String[] strings = {"A", "B", "C", "D"};
 Collection list = java.util.Arrays.asList(strings);

 / * Start traversing * /
 for (Object str : list) {
     System.out.println (str); / * sequentially outputs "A", "B", "C", "D" * /
 }

As used herein for the cycle, it will be seen during the compilation is of the form:

Equivalent code is a simple way of traversing the Collection: 6 Listing

 / * Create a Collection * /
 String[] strings = {"A", "B", "C", "D"};
 Collection stringList = java.util.Arrays.asList(strings);

 / * Start traversing * /
 for (Iterator variable name B = list.iterator (); variable name B .hasNext ();) {
     Object str = variable name .next B ();
     System.out.println (str); / * sequentially outputs "A", "B", "C", "D" * /
 }

Here the "variable name B" is also a confusion of names by the compiler will not automatically generated.

Because during compilation, J2SE 1.5 compiler will loop for this form, as a form corresponding to the traditional, there is no fear of performance problems arise.

Not "foreach" and "in" reasons

Java uses "for" (rather than the more specific sense of "foreach") to direct this general called "foreach loop" recycling, and use the ":" (rather than the more specific meaning "in") to split the name and the object to be traversed loop variable. The main reason for this is to avoid the introduction of new keywords, causing compatibility issues - in the Java language, is not allowed to use keywords as variable names, although the case of using the "foreach" This name not very much, but "in" is a name often used to represent the input stream (for example java.lang.System class, there is a name called "in" a static property, which means "the standard input stream").

Indeed through clever design syntax, so only the keywords have special meaning in a particular context, to allow them to be used as a common identifier. However, this would complicate the syntax of strategy, it has not been widely adopted.

 "For-each loop" of history

"For-each loop" is not a control structure only recently. In 1979 the official release of the Bourne shell (the first of a mature UNIX command interpreter) has been included in this control structure (circulation "for" and "in" to guide, with the body of the loop "do" and "done "to identify).

2, to prevent the loop variable modification within the loop body

By default, the compiler is to allow for circulation of the second loop, the loop variable re-assignment. However, because this approach had no effect on the situation outside the loop, but also likely to cause difficulties in understanding the code, it is generally not recommended.

Java provides a mechanism to put the ban such operations during compilation. Specific method, in the previous cycle plus a variable type "final" modifier. Thus, the assignment of the loop variable in the loop body, it will lead to a compiler error. With this mechanism, you can effectively eliminate intentionally or unintentionally be "modified loop variable in the loop body" of the operation.

Listing 7: Prohibition reassigned

 int[] integers = {1, 2, 3, 4};
 for (final int i : integers) {
     i = i / 2; / * compile-time error * /
 }

Note that this only prohibits the loop variable re-assignment. Assigns them the loop variable, or call to make changes in the content of the loop variable method, is not prohibited.

Listing 8: allows you to modify the state

Random[] randoms = new Random[]{new Random(1), new Random(2), new Random(3)};
 for (final Random r : randoms) {
     r.setSeed (4); / * all Random object set to use the same seed * /
     System.out.println (r.nextLong ()); / * same seed, the first result is the same * /
 }

3. Type compatibility problems

To ensure the loop variable energy, have been security assignment at the time of the beginning of each cycle, J2SE 1.5 has some restrictions on the type of the loop variable. Under these restrictions, the type of the loop variable can have a number of options:

Variable cycle type may be the type of element and the object to be traversed in the same. For example, an int type array to traverse a loop variable int [] type with an Object to traverse a loop variable Collection like.

Listing 9: the same type of use and to be traversed loop variable elements in the array

int[] integers = {1, 2, 3, 4};
 for (int i : integers) {
     System.out.println (i); / * sequentially outputs "1", "2", "3", "4" * /
 }

Listing 10: the same type of use and to be traversed loop variable elements in Collection

Collection< String> strings = new ArrayList< String>();
strings.add("A");
strings.add("B");
strings.add("C");
strings.add("D");
for (String str : integers) {
System.out.println (str); / * sequentially outputs "A", "B", "C", "D" * /
}

Variable cycle type may be superior to the element type of an object to be traversed in. For example, a variable of type int loop through the array to a byte [] type, with variable loop type Object to traverse a Collection <String> (String all the elements are Collection) and the like.

Listing 11: Using superior type of element objects to be traversed in a loop variable

String[] strings = {"A", "B", "C", "D"};
Collection< String> list = java.util.Arrays.asList(strings);
for (Object str : list) {
System.out.println (str); / * sequentially outputs "A", "B", "C", "D" * /
}

Type variable cycle can be automatically converted there is a relationship between the type element and the object to be traversed in. J2SE 1.5 contains a mechanism "Autoboxing / Auto-Unboxing", allowing the compiler when necessary, automatically convert between types and their basic parcel (Wrapper Classes). Thus, with the type Integer a loop through the array variable int [] type, or a variable byte type of loop to traverse a Collection <Byte>, it is also possible.

Listing 12: energy and type of objects to be traversed automatic conversion elements of the loop variable of type

int[] integers = {1, 2, 3, 4};
for (Integer i : integers) {
System.out.println (i); / * sequentially outputs "1", "2", "3", "4" * /
}

Note that the "element type" here said, is to be traversed by the object decision - if it is an array [] type Object, then the type element is Object, even if there is a String object is loaded in this way.

Collection element types can be defined

Up until J2SE 1.4, the type that can be saved in the Collection object has failed to define a Java program - all of them are seen as the most generic Object object. Until after the J2SE 1.5, the introduction of the "generics (Generics)" mechanism, the problem was solved. Can now Collection <T> to represent T Collection of all types are element.

Guess you like

Origin www.cnblogs.com/lqywong/p/10968582.html