1.数组
//声明一维数组
int student[ ];
int[ ] student;
//声明二维数组
int score[ ][ ];
int [ ][ ] score;
//数组的创建
student = new int [50];
score = new int [50] [4];
//类的一般创建格式
s = new String ( "student" );
//数组的复制
int num [ ] = { 2, 3, 5, 1 };
int numcopy[ ]=num;
numcopy[2 ]=5;
-
一旦数组被创建,数组的大小就不能改变。
-
如果在程序运行期间,需要对数组的大小进行扩展,通常需要使用另一种数据对象Vector。
2.字符串
用String类来表示
//(1)length()方法:
String s = "student";
int len=s.length();
//(2)子串
String s = "I am a Chinese";
String subs;
subs = s.substring (7);//得到子串subs为”Chinese”
//(3)字符串的比较
String tom = "my name is tom";
String jane = "my name is jane";
tom.equals(jane);//返回false。表示不相等
tom.compareTo(jane) ; //返回一个负整数,因为第一个不相等的字符t和j相比,t在j的后面;如果返回0,表示相等;如果返回一个正整数,表示tom和jane第一个不相等的字符,tom的在jane的前面。
//(4)字符串连接
String s1 = “ I am”,s2,s3,s4;
String s2 = "a Chinese";
s3 = s+s2 = "I am a Chinese";
s4=s3 + 24 = "I am a Chinese24";//整数型24将会自动转换为字符串。
//(5)字符串检索
//字符串检索是指判断一个字符串是否包含某一个字符或子串,如果有,返回其位置,如果没有,返回一个负数。
String s = "I am a Chinese";
s.indexOf("Chinese" ),>>>返回7;
s.indexOf('a'); >>>返回2;
//(6)字符串转换为数值
//如果一个字符串都是数字字符,可把它转换成相应的数值。
//转换为整型:
String s = "21";
int x;
x= Integer.parseInt (s);
//转换为浮点型
String s = "22.124";
float f;
f = Float.valueOf(s).floatValue();
//(7)数值转换为字符串
String s;
int n = 24;
s = String.valueOf ( n );



