1、计算一组有多少个术语个数
int 名称=要计算的组名.length
public class SimpleArray {
public static void main(String[]args){
String[] wordListone={"24/7","nmultiTier","30000 foot","B-to-B","win-win","front-end","web-based"};
String[] wordListtwo={"empowered","sticky","value-added","oriented","centric"};
int[] wordListthree={1,3,4,5,6};
int oneLength=wordListone.length;//计算一组有多少个
int twoLength=wordListtwo.length;
int threeLength=wordListthree.length;
System.out.println(oneLength);
System.out.println(twoLength);
System.out.println(threeLength);
}
}
2、产生随机数字的方法
public class Random {
public static void main(String[]args){
int c=(int)Math.random();//转换,将double类型转换为int类型
double x=Math.random();//产生一个随机数,仅仅会介于0和1之间
int y=5;
int z=(int)(x*y);//随机数与定义的数字相乘,转换
System.out.println(c);
System.out.print(z);
}
}
3、定义数组
public static void main(String[]args){
String[] wordListone={"24/7","nmultiTier","30000 foot","B-to-B","win-win","front-end","web-based"};//注意在输入数组内容的时候不要回车
String[] wordListtwo={"empowered","sticky","value-added","oriented","centric"};
int[] wordListthree={1,3,4,5,6};
int[] shuzi;
shuzi=new int[7];//创建一个大小为7的数组
shuzi[0]=1;
shuzi[1]=3;
shuzi[2]=8;
shuzi[3]=10;
shuzi[4]=10;
shuzi[5]=922;
shuzi[6]=23;//为数组赋值
}
创建对象数组
对象[] 对象名称;
对象名称=new 对象[数字];
Dog[] pets;//Dog是对象,pets是名称
pets=new Dog[3];//给pets赋值
pets[0]=new Dog();
pets[1]=new Dog();
pets[2]=new Dog();//我认为,就是一次性定义了多个对象
pets[0].name="钢镚";//为第一个对象的变量name赋值
pets[0].bark();//第一个对象调用bark方法
一个创建对象数组的具体例子
普通类
class NormalClass {
String name;
void bark(){
System.out.println(name+" "+"says Ruff!");
}
void eat(){
}
void chaseCat(){
}
}
测试类
public class TestClass {
public static void main(String[]args){
NormalClass dog1=new NormalClass();
dog1.bark();
dog1.name="Bart";
NormalClass[] pets;
pets=new NormalClass[3];
pets[0]=new NormalClass();
pets[1]=new NormalClass();
pets[2]=dog1;
pets[0].name="Fred";
pets[1].name="Marge";//这部分以上很重要
System.out.println("Last dog's name is"+" "+pets[2].name);
int x=0;
while(x<pets.length){
pets[x].bark();
x=x+1;
}
}
}
又一个定义对象数组的小例子
public class ExerciseTestClass2 {
public static void main(String[]args){
int y=0;
String [] islands=new String [4];//定义了一个String类型的对象数组
islands[0]= "Bermuda";
islands[1]="Fiji";
islands[2]="Azores";
islands[3]="Cozumel";//为每一个对象数组赋值
int[] index=new int[4];//定义一个整数类型的数组
index[0]=1;
index[1]=3;
index[2]=0;
index[3]=2;//为数组的每一个位置赋值
while(y<4){
int ref;//定义一个整数变量
ref=index[y];//将整数型数组赋给ref这个整数变量
System.out.print("island ="+" ");
System.out.println(islands[ref]);//输出的时候按照index中各个位置的值顺序输出,类似嵌套的赶脚
y=y+1;
}
又又一个数组定义的例子,但它有个操作很迷
public class ExerciseTestClass3 {
public static void main(String[]args){
int x=0;
ExerciseNormalClass3 [] ta=new ExerciseNormalClass3[4];
while(x<4){
ta[x]=new ExerciseNormalClass3();
ta[x].height=(x+1)*2;
ta[x].length=x+4;
ta[x].setArea();
System.out.print("triangle"+x+",area");
System.out.println("="+ta[x].area);
x=x+1;
}
int y=x;//这个操作太迷了,我不懂
x=27;
ExerciseNormalClass3 t5=ta[2];
ta[2].area=343;
System.out.print("y= "+y);
System.out.println(", t5 area= "+t5.area);
}
}
数组中的定位
数组名[位置,0表示第一个]
4、定义方法
定义一个方法的语句: void 方法名(){方法语句块}
一般在普通类中进行不需要用main()
void bark(){
System.out.println("Ruff!Ruff!");
}//定义了一个方法bark()
5、建立对象
建立一个对象的语句:普通类名 对象类名=new 普通类名()
一般是在测试类中进行
SimpleNormalClass s=new SimpleNormalClass();//定义一个对象名为s,这个对象的建立必须与普通类的名字相同
6、调用方法
一个对象调用一个方法的语句:对象名.方法名();
s.bark();//调用这个方法
7、声明变量
声明变量的语句:类型 名称;
int count;
8、类、方法、变量命名规则
-
名称必须以字母、下划线(_)或者$符号开头,不能用数字开头
-
除了第一个字母,其它都可以用数字
-
在符合上述两条规则的基础上,还要避开Java的保留字
Java中的保留字:boolean、byte、char、double、float、int、long、short、public、static、private、protected、abstract、final、native、strictfp、synchronized、transient、volatile、if、else、do、while、switch、case、default、for、break、continue、assert、class、extends、implements、import、instanceof、interface、new、package、super、this、catch、finally、try、throw、throws、return、void、const、goto、enum -
Java保留字的解释:
break:无条件立刻跳出循环
9、变量的比较
- 用“==”来比较两个primitive主数据类型或判断两个引用是否引用同一个对象
int a=3;
byte b=4;
if(a==b){
//true}
Dog one=new Dog();//定义一个名为one的对象
Dog two=new Dog();//定义一个名为two的对象
Dog three =one;//定义一个等同于a的对象
if(one==three){
//true
}
if(one==two){
//false
}
- 使用equals()来判断两个对象是否在意义上相等,例如两个String类型的数据
String a="nihao";
String b="nihao";
if(a.equals(b)){
//true}
10、变量类型的转换
- 将String类型(字符型)转换为数值型
代码结构:Java内建类.parse数值类型(待转换的字符型值)
PS:parse这种方法仅能够将String类型转换为数值型,不可颠倒,也不能实现数值类型之间的转换。Java内重建类:转换为int型则为Integer,转换为float则为Float。注意大小写
public class PracticeNormalClass {
public static void main(String[]args){
String x="36";
int y=Integer.parseInt(x);//将String类型转换为int类型
System.out.println("y的最终结果是:"+" "+y*3);
float f=Float.parseFloat(x);//将String类型转换为float类型
System.out.println("f的最终结果是:"+" "+f*3);
double d=Double.parseDouble(x);//将String类型转换为double类型
System.out.println("d的最终结果是:"+" "+d*3);
}
}
11、后递增
代码结构:数值类型变量++
后递增效果等同于该变量加1
ex:x++;等同于 x=x+1