//当break z 返回到z的时候,会不会再次开始循环?
public void go(){
String o = "";
z:
for(int x=0; x<3; x++){
for(int y=0; y<2; y++){
if(x == 1) break;
if(x==2 && y==1) break z;
o = o + x + y;
}
}
System.out.println(o);
}
//5是怎么来的?
public class Test {
public static void main(String[] args) {
String[] colors = { "blue", "red", "green", "yellow", "orange" };
Arrays.sort(colors);
int s2 = Arrays.binarySearch(colors, "orange");
int s3 = Arrays.binarySearch(colors, "violet");
System.out.print(s2 + "" + s3);
}
}
//为什么编译失败?
public class Test {
public static void main(String[] args) {
new Test.go("hi", 1);
new Test.go("hi", "world", 2);
} public void go(String... y, int x) {
System.out.print(y[y.length - 1] + " ");
}
}
public void go(){
String o = "";
z:
for(int x=0; x<3; x++){
for(int y=0; y<2; y++){
if(x == 1) break;
if(x==2 && y==1) break z;
o = o + x + y;
}
}
System.out.println(o);
}
//5是怎么来的?
public class Test {
public static void main(String[] args) {
String[] colors = { "blue", "red", "green", "yellow", "orange" };
Arrays.sort(colors);
int s2 = Arrays.binarySearch(colors, "orange");
int s3 = Arrays.binarySearch(colors, "violet");
System.out.print(s2 + "" + s3);
}
}
//为什么编译失败?
public class Test {
public static void main(String[] args) {
new Test.go("hi", 1);
new Test.go("hi", "world", 2);
} public void go(String... y, int x) {
System.out.print(y[y.length - 1] + " ");
}
}
class Super {
protected static int a; protected Super(int a) {
this.a = a;
}
}class Sub extends Super {
public Sub(int a) {
super(a);
} public Sub() {
this(a); //这个方法应该报错的,但是,它没有。
}
}
2.这个要懂Arrays.binarySearch函数,在数组里能找到,就返回其位置;找不到的返回其假定的插入点-1;
所以s2=2,s3=-5
static String o = ""; public static void main(String[] args) {
z: for (int x = 2; x < 7; x++) {
if (x == 3)
continue;
if (x == 5)
break z;
o = o + x;
}
System.out.println(o);
//打印结果是24,从这里看来似乎是break z;之后就停止了进入for,直接打印了出来。
}
}
如果找不到就假定插入点为-1,那么-5是怎样来的呢?能不能说的具体一点。
还有帮我看看下面这个报错的原因,谢谢。public class Test {
static String o = ""; public static void main(String[] args) {
z: o = o + 2;
for (int x = 3; x < 8; x++) {
if (x == 4)
break;
if (x == 6)
break z; //为什么编译出错?
o = o + x;
}
System.out.println(o);
}
}
第二题:你调用的是Arrays.sort(Object[] a,Object key),看API说明如下:
如果它包含在数组中,则返回搜索键的索引;否则返回 (-(插入点) - 1)。插入点 被定义为将键插入数组的那一点:即第一个大于此键的元素索引,如果数组中的所有元素都小于指定的键,则为 a.length。注意,这保证了当且仅当此键被找到时,返回的值将 >= 0。
你排序String[]是按首字母>次字母……的自然顺序将colors排序为:blue、green、orange、red、yellow。violet的插入点位yellow之后,可以由api说明得知,返回(-4)-1,即-5.第二题打印输出应为2-5。
第三题给你一个正确的程序,自己对照看错误在哪里:public class Test {
public static void main(String[] args) {
new Test().go(1,"hi" );
new Test().go(2,"hi", "world" );
} public void go(int x,String... y) {
System.out.print(y[y.length - 1] + " ");
}
}