Home > Back-end >  Why don't the following array index crossed
Why don't the following array index crossed

Time:11-24

Int [] arr=new int [10].
For (int I=20; I & lt; Arr. Length; I++) {
Arr [I]=I;
}

For (int a: arr) {
System. The out. Print (a + "\ t");
}
Iterate through the result is ten 0;
Solve??????

CodePudding user response:

The first for loop didn't even enter well, why not consider code problem, is to ask questions

CodePudding user response:

Arr. The length is 10, the first for the first condition is that I=20 & lt; 10 and not to enter the first for
Should define the int I=0

CodePudding user response:

Thank you very much! Novice small white send blessings!

CodePudding user response:

The first cycle not in; The second loop variable initialization int the default value is 0

CodePudding user response:

I++ into - I try

CodePudding user response:

This code is no problem, you of the for loop to start condition is false, don't perform, which come of crossing the line?

CodePudding user response:

Did not enter a for loop,

CodePudding user response:

The first line
Int [] arr=new int [10].
This code will create an array, each element will be given an initial value of 0
For (int I=20; I & lt; Arr. Length; I++) {
Arr [I]=I;
}
Here will be initialized I value of 20 in execute judgment statement I & lt; Arr. Length
20 & lt; 10 is false loop over the elements in the array are clearly still value is 0
So can output 10 0

CodePudding user response:

Two for loop, the second for loop, don't use the array subscript, so, no chance of error,
The first for loop, you should be aware of the specific steps for loop execution:
1. Perform the first expression before the semicolon, namely int I=20;
2. Determine whether to enter circulation, the second expression before the semicolon, namely the I & lt; Arr. Length; Obviously, with step 1, I=20, then, certainly does not set up the conditions, so, can't enter the loop body,
Because the program doesn't even have a loop into, so, the loop body of arr [I]=I; Statement didn't get the chance to perform, so, not at the Angle of the cross-border abnormalities,

May I make myself clear?
  • Related