数组
1.
|
一维数组 a[0]=100 1001
int[] a; a+0*4=1001 1002
int[] a={1,2,3,4}; 1003
int[] a=new int[5]; 1004
int[] a=new int[]{5,6,7}; 1005
a[1] a+1*4=1005
a[2] a+2*4=1009
2.为什么数组下标从0开始:数组变量本身拿着内存地址的一第片地址。
3.i.length 数组长度
4. for(h:i1){ //打印数组
System.out.print(h);
}
= for(int u=0;u<i1.length;u++){
Int h=i1[u];
System.out.println(h);
}
5.冒泡排序
int[] i={10,50,66,7,12,78};
for(int a=0;a<i.length-1;a++){
for(inty=i.length-1;y>a;y--){
If(i[y]<i[y-1]) {
int h=i[y];
i[y]=i[y-1];
i[y-1]=h;
}
|
}
}
for(int j:i){
System.out.println(j+” ”);
}
6.二维数组:int[][] a= new int[3][3];
//int [][]a=new int [][]{{10,20},{30,40}};
int [][]a=new int[2][];
a[0]=new int[]{10,50,60};
a[1]=new int[]{1,45,2,52,22,21};
/for(int[] list:a){ //行
for(int u:list){ //列
System.out.print(u+” ”);
}
System.out.println(“ ”);
}/
for(int i=0;i<a.length;i++){
for(int u=0;u<a[i].length;u++){
System.out.print(a[i][u]+” ”);
}
System.out.println(“ ”);
|
}
7.eg.int[][].i=newint[3][];
i[0].length i[0][2]=100
i[0]=new int[3];
i.length i[1]=new int[2];
|
i[2]=new int[1];
i[1].length
8.①对象数组
class Dog{
String name;
}
① Dog[] ds=new Dog[]{new Dog(),new Dog(),new Dog()}
旺财 笨笨
② Dog[] ds=new Dog[3];
ds[o]=newDog();
|
|
|
ds[1]=new Dog();
ds[2]=new Dog();
ds[0].name=”旺财”;
|
|
|
ds[1].name=”笨笨”;
③ Dog[] ds=new Dog[3];
ds[0]=a1;
|
ds[1]=a2;
ds[2]=a3;
ds[1].name=”旺财”;
|
System.out.println(a2.name);
9. ②数组引用
Dog d1=new Dog();
d1.name=”旺财”;
Dog d2=new Dog();
|
d2.name=”旺财它哥”;
|
Dog[] ds=new Dog[]{d1,d2};
ds[0].nane=”旺财二代”;
|
System.out.println(d1.name);
结果为旺财二代
|
10.d:/>java
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
有 java.exe
11.接受外界参数(String[] args)
12.在DDS中”*”是目录的意思。
13.封装:面向用户更简单,面向属性更安全。
14.java权限修饰符 public protectedprivate 置于类的成员定义前,用来限定其他对象对该类对象的访问权限。
Public 任何地方
Protected 在不同包的子类中可用
Default 只可以被同一个包内部的类访问
Private 只能访问类的内部
15. 封装的两个方法 set和get
16.private String name;
Public void setName(String n){
name=n;
}
Public voidgetName(){
return name;
}
private String name1;
private String name2;
public void setName(String n1,String n2){
name1=n1;
name2=n2;
}
Public void getName(){
return (name1+name2);
}
安全验证
publicvoid setXy(int x1,int y1){
if(x1<=-1|| y1<=-1){
System.out.println("wrong```");
}else{
x=x1;
y=y1;
}
数组的封装:
private String[] colors=new String[10];
publicvoid setColors(String[] cs){
colors=cs;
}
publicString[] getColors(){
return colors;
}
public void addcolor(int index,String color){
colors[index]=color;
}
publicint getColorsSize(){
return colors.length;
}
Boolean类型的封装:
public boolean state;
publicvoid setState(boolean s){
state=s;
}
publicboolean isState(){
return state;
}