回顾下:
5.8 数组初始化
int[] array=new int[9];
int[] arrsy={1,2,3,4};
eg:数组的复制
public class rt{
public static void main(String[] args){
int[] a={1,2,3};
int[] b;
b=a;
for(int i=0;i<a.length;i++)
b[i]=a[i]+10000;
for(int i=0;i<b.length;i++)
System.out.println(a[i]);
}
}
结果:
10001
10002
10003
分析:数组a和数组b在同一个内存空间。
eg:数组的创建是在动态运行时开始的。
import java.util.*;
public class rt{
public static void main(String[] args){
int[] a;
Random rand=new Random();
int r=rand.nextInt(99);
a=new int[r];
System.out.println("the length of a="+a.length);
System.out.println(Arrays.toString(a));
}
}
结果:
the length of a=5
[0, 0, 0, 0, 0]
分析:数组会自动初始化初值,为0;
定义的时候初始化:int[] arrsy=new int[rand.nextInt(99)];
eg:给数组赋值
import java.util.*;
public class rt{
public static void main(String[] args){
int[] a;
Random rand=new Random();
int r=rand.nextInt(99);
a=new int[r];
for(int i=0;i<a.length;i++)
a[i]=rand.nextInt(9);
System.out.println("the length of a="+a.length);
System.out.println(Arrays.toString(a));
}
}
结果:
the length of a=14
[1, 5, 0, 4, 3, 0, 8, 5, 6, 8, 0, 2, 6, 2]
eg:类数组,Integer是一个类而不是基本类型。
8个基本类型:浮点型float,double ;布尔型boolean;字符型char;整数型int ,long,byte,short。
import java.util.*;
public class rt{
public static void main(String[] args){
Integer[] a;
Random rand=new Random();
int r=rand.nextInt(99);
a=new Integer[r];
for(int i=0;i<a.length;i++)
a[i]=rand.nextInt(9);
System.out.println("the length of a="+a.length);
System.out.println(Arrays.toString(a));
}
}
结果:
[4, 3, 8, 0, 7, 2, 8, 2, 5, 2, 1, 1, 8, 2, 2, 1, 7, 2, 7, 5, 0, 0, 0, 0, 7, 7, 8, 0, 2, 5, 0, 0]
eg:
import java.util.*;
public class rt{
public static void main(String[] args){
Integer[] a={1,new Integer(2),3};
System.out.println(Arrays.toString(a));
Integer[] b=new Integer[]{3,new Integer(2),1};
System.out.println(Arrays.toString(b));
}
}
结果:
[1, 2, 3]
[3, 2, 1]
eg:创建一个String数组,传递给另一个main()方法。
import java.util.*;
class test{
public static void main(String[] args){
for(String s:args)
System.out.println(s+" ");
}
}
public class rt{
public static void main(String[] args){
test.main(new String[]{"a","b","c"});
}
}
结果:
a
b
c
5.8.1 可变参数列表
所有的类继承于Object类,可以创建Object类型的数组。
class test{}
public class rt{
public static void printarray(Object[] args){
for(Object o:args)
System.out.print(o+" ");
}
public static void main(String[] args){
printarray(new Object[]{new Integer(99),new Double(11.00)});
printarray(new Object[]{"one","two","three"});
printarray(new Object[]{new test(),new test(),new test()});
}
}
结果:99 11.0 one two three test@7ffe01 test@fd13b5 test@118f375
分析:打印对象时,答应类的名字和十六进制数字。
eg:可变参数列表
class test{}
public class rt{
public static void printarray(Object... args){
for(Object o:args)
System.out.print(o+" ");
}
public static void main(String[] args){
printarray(new Object[]{new Integer(99),new Double(11.00)});
printarray(new Object[]{"one","two","three"});
printarray(new Object[]{new test(),new test(),new test()});
printarray((Object[])new Integer[]{1,2,3});
printarray();
}
}
结果:99 11.0 one two three test@7ffe01 test@fd13b5 test@118f375 1 2 3
(Object[])
强制类型转化。
装箱 和 拆箱 的知识。
public class rt{
public static void function(int i,String... args){
System.out.print("i="+i+" ");
for(String s:args)
System.out.print(s+" ");
System.out.println();
}
public static void main(String[] args){
function(1,"one");
function(2,"two","three");
function(0);
}
}
结果:
i=1 one
i=2 two three
i=0
public class rt{
static void function(Character... args){
System.out.print(args.getClass());
System.out.println("length="+args.length);
}
static void func(int... args){
System.out.println(args.getClass());
System.out.println("length="+args.length);
}
public static void main(String[] args){
function('a','b');
function();
func(1);
func();
System.out.println("int[]:"+new int[0].getClass());
}
}
结果:
class [Ljava.lang.Character;length=2
class [Ljava.lang.Character;length=0
class [I
length=1
class [I
length=0
int[]:class [I
分析:getClass()方法属于Object,它产生对象的类,在打印该类时,看到该类类型的编码字符串。前面的"["表示这是一个数组,'I'表示int型。
public class rt{
public static void f(Integer... args){
for(Integer i:args)
System.out.print(i+" ");
System.out.println();
}
public static void main(String[] args){
f(new Integer(1),new Integer(2));
f(1,2);
f(1,new Integer(2));
}
}
结果:
1 2
1 2
1 2
分析:在相同的参数列表中有不同类型,
5.9 枚举类型
关键字enum
public class rt{
public enum test{morning,afternoon,night};
public static void main(String[] args){
test shangwu=test.morning;
System.out.println(shangwu);
}
}
结果:morning
public class rt{
public enum test{morning,afternoon,night};
public static void main(String[] args){
test shangwu=test.morning;
System.out.println(shangwu);
for(test t:test.values())
System.out.println(t+" "+t.ordinal());
}
}
结果:
morning
morning 0
afternoon 1
night 2
分析:创建enum时,会自动创建toString()方法,以方便打印。
ordinal()方法,表示enum中元素顺序;static values()方法,按照顺序,产生这些常量构成的数组。
eg:在数组中使用enum
public class rt{
public enum test{morning,afternoon,night};
test t;
public rt(test t){this.t=t;}
public void describe(test t){
System.out.print("your choice is:");
switch(t){
case morning:System.out.println("zaoshang");
break;
case afternoon:Ststem.out.println("xiawu");
break;
case night:System.out.println("wangshang");
break;
default:System.out.println("error");
}
}
public static void main(String[] args){
rt am=new rt(test.morning);
rt pm=new rt(test.afternoon);
rt no=new rt(test.night);
am.describe();
pm.describe();
ni.describe();
}
}
结果:
rt.java:10: 错误: 程序包Ststem不存在
case afternoon:Ststem.out.println("xiawu");
^
rt.java:21: 错误: 无法将类 rt中的方法 describe应用到给定类型;
am.describe();
^
需要: test
找到: 没有参数
原因: 实际参数列表和形式参数列表长度不同
rt.java:22: 错误: 无法将类 rt中的方法 describe应用到给定类型;
pm.describe();
^
需要: test
找到: 没有参数
原因: 实际参数列表和形式参数列表长度不同
rt.java:23: 错误: 找不到符号
ni.describe();
^
符号: 变量 ni
位置: 类 rt
4 个错误
第6章 访问权限控制