Enhanced for Loop)The so-called "enhanced for loop" mainly targets containers. When this feature is used, developers canThe logic of traversing the container is handed over to the compiler for processing. For example, the following code:Void cancelall (collection C ){For (iterator I = C. iterator (); I. hasnext ();){Timertask TT = (timertask) I. Next ();TT. Cance
Writing a program is to create an array of strings that, when traversed using a foreach statement, immediately interrupts the loop if it finds that the array contains the string "Eagle". Then you create a two-dimensional array of integer types, loop through the two-layer foreach statement, and when you find the first array element less than 60, you immediately interrupt the entire two-tier
Introduction to new features in JDK5.0:generic type (generics);Enhanced "for" loops (enhanced for loop);Automatic packing/automatic unpacking (autoboxing/unboxing);Type safety enumeration (type safe enums);Static import (statically import);Variable parameters (Var args);Enhanced for Loop:The addition of the For-each loop simplifies the traversal of the collectionIts syntax is as follows:for (type Element:ar
When I wrote it completely did not think how to reduce the number of moves, from the internet to find a completely didn't think of the method of trickery, and did not think of the way to reduce the number of moves.The trickery does not move with the array and outputs directly;Displacement is 1234 the first tail 22 transposition into 4321, 56 to 65, and then 43,216,522 transposition is exactly 561234, how did this come to mind?Another point is that if the array length is 5, the displacement is 6,
for (int j = 0; J Execute TODO}1. First the variable j is initialized to 02. Then the value of j=0 is compared with newsize, and if true, the contents of {} are executed, if False, the loop body is popped out3.{} execution completed, then J + +4.j++ execution completes, and then compared with newsize, if true, then execute {} contents, if False, jump out of the loop body5.
This article briefly introduces how to use the for loop statement in java. If you need to solve the for statement, you can refer to it.
I. nested application of statements
Statement nesting form. In fact, there are still statements in the statement. There are various forms without fixed formats and routines.
1. Print even numbers
The Code is as follows:
Copy code
For (int x = 1; x {I
Java JButton button monitoring, when more than one button to do things can be used for the loop (such as calculator 1-9) Save effort.The variables can only be final or static when listening, but these are obviously not possible in the For loop, so we add a final variable in the loop.Hey, easy to fix.Package Com.xinbo;Import Java.awt.FlowLayout;Import Java.awt.Gri
Several circular accesses to the collection object list in Java are summarized as follows
1 Classic for loop
Java code
Public
Static
voidMain (string[] args) {
listnew ArrayList ();
List.add ("123");
List.add ("Java");
List.add ("Java ee");
System.out.println ("========= Cl
1 Case Description
One day, on the Javaeye to see an interview question, the title is this: please optimize the following code
Java code for (int i = 0; i (Note: In order to be consistent with the contents of the following, the original topic has been partially modified)
2 Case Study
From the code given, no matter how optimized, the number of testfunction execution is the same, and there is no possibility of optimization in this part. Then, the op
Java for loop detailed description
Import java.util.*;
public class enhancedfor{
public static void Main (string[] args) {
Int[] arr = {1, 2, 3, 4, 5};
for (int i:arr) {
System.out.println (i);
}
Collection C = new ArrayList ();
C.add (New string ("AAA"));
C.add (New string ("BBB"));
C.add (New string ("CCC"));
For (object o:c) {
System.out.println (o);
}
}}
jdk1.5 only after the reinfo
interface is implemented inside the list interface, providing a developer with a iterator () to get a iterator object for the current list object.[Java]View PlainCopy
iterator
while (Slistiterator.hasnext ()) {
String e = Slistiterator.next ();
if (E.equals ("3")) {
Slistiterator.remove ();
}
}
The above is also correct, and the second option is recommended.Both implementations are poorly implemented, and the se
InJavaTo jump out of multiple loops, you can define a label before an External Loop statement, and thenCodeUse a break statement with a label to exit the outer loop. For example:
OK:
For (INT I = 0; I
{
For (Int J = 0; j
{
System. Out. println ("I =" + I + ", j =" + J );
If (j = 5) Break OK;
}
}
However, I personally think that the label met
>a;i--) {//Outputs 5 spaces for a loop, reducing this to 1;Systen.out.print (""); The loop output space is not bhakta;} Systen.out.println ("*"); The first line ends with a newline so use println;} else{for (int i = 5;i>a;i--) {//Outputs 5 spaces for a loop, reducing this to 1;Systen.out.print (""); The loop output spa
triangular patterns print as follows");System.out.print ("------------------" + "\ n");Fun1 ();System.out.print ("------------------" + "\ n");Fun2 ();System.out.print ("------------------" + "\ n");Fun3 ();System.out.print ("------------------" + "\ n");Fun4 ();System.out.print ("------------------" + "\ n");}} Individual self-writing for multiple loops, using 4 functions. Each function prints a graph, and finally calls together 4 functions Print out the 4 triangles above. This is just a simpl
If an exception is caught in the loop, but you do not want to interrupt the loop at the same time:for (int i=-1;itry {SYSTEM.OUT.PRINTLN ("result =" +i);if (1/i!= 0) {SYSTEM.OUT.PRINTLN ("result =" +i);}} catch (Exception e) {E.printstacktrace ();}}The console results are:Result =-1Result =-1Result =0Java.lang.ArithmeticException:/By zeroAt Mytask.run (ttt.java:23)At Java.util.TimerThread.mainLoop (timer.ja
1 Importjava.util.ArrayList;2 3 Public classMain {4 5 Public Static voidMain (string[] args) {6arraylistNewArraylist();7List.add (1);8List.add (2);9List.add (3);TenList.add (4); One A for(intn:list) { -n = 4;//This approach does not change the elements of the list - //System.out.println (n); the } - /*Enhanced for-loop*/ - for(intm:list) { -System.out.print (M + ""); + } - System
The For loop in Java has a--foreach notation (typically used only to traverse the entire array without worrying about cross-border issues).1.1) General wording:Package Foreach.main.sn;public class foreach {public static void main (string[] args) {int[] arr = {1,2,3,4,5};for (int i = 0; i 1.2) foreach notation: (Note theTis a temporary variable value that is used to hold the array value that is currently tra
Java for Loopsimple for loopFor-each or enhanced for loopLabeled for loopExampleImport Java.util.arrays;import Java.util.list;import Org.junit.test;public class Forexample {@Test public void Test () {//Simple for loop for (int i = 1; i Java for Loop
Today I learned the Java for Loop and wrote a 99 multiplication table with this to share with you. Good stuff to know how to share the amount.The source code is as follows:for (int i=1;ifor (int j=1;jSystem.out.print (i+ "*" +j+ "=" + (i*j) + "\ T");}System.out.println ("");}This article is from the "Hughst" blog, make sure to keep this source http://hughst.blog.51cto.com/9457210/1655708The
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.