类的数组

来源:互联网 发布:lcd1602数据手册 pdf 编辑:程序博客网 时间:2024/06/06 01:02


1、引用类型数组:与常用数组类型相同,仅仅在申明的时候有直接可以用和需要再次申明的差异;
申 明: Student [ ] allStus = new Student [ 5 ]; 产生了5个学生对象的引用地址,没有产生学生对象;
使 用:allStus[ i ] = new Student( ); 每个对象被单独创建;并不遵守地址连续;
调用方法, Stus[ i ] .setName(“张三”/ name);
插入图
赋值方法
UserBean [] allUsers = new UserBean[ 5 ];
for( int i = 0; i < allUsers.length; i++){
System.out.println( "请输入第*个账户名" )
System.out.println( "请输入第*个账户密码" )
allUsers[ i ] = new UserBean("zhang" , "123456");
}


遍历方法:专门针对集合进行的遍历 for-each 循环; 【each 是每一个的意思;】
for( Student tmp : allStus ){
tmp.report();
}
【注释】:将对象数组中的每个对象赋值给tmp,然后依次调用每个对象的 report 方法;
该方法只能遍历,不能输入值,也没有确定的下标

数组的冒泡算法
private void rank(){
Studend maxStudend = new Studend();
for(int j = 0; j < studend.length; j++){
for (int i = 0; i < studend.length-1; i++) {
if( studend[i].getScore()< studend[i+1].getScore()){
maxStudend = studend[i];
studend[i] = studend[i+1];
studend[i+1] = maxStudend ;
}
}
}

引用数组遍历打印:
for( Student stu : allStus ){
System.out.println( stu.getName() + " " + stu.getScore() );
}
}
引用对象之间交换数据可以通过直接相等进行赋值转换;


2、如何解决,数组大小不可变的问题?能否自定义数组类,让它任意变大变小
//定义一个数组长度为10的数组array[];
//将外部输入数据放入这个数组中;并在全局变量中定义一个下标Index, 放在方法中 this.index++; 并且用this.index代表在数组中放了多少个有效数组元素; 也可以用size;
//一旦判断要放的数组,放满了,对array扩大10个位置的数组长度;
//将类中的数组复制给这个数组,然后将这个数组的指针指向该数组;最后this.array = new
//添加元素
/**
* 本方法完成元素的添加动作
* @param value 代表要放入到数组中的元素
*/
public void add(int value){
if(array.length == index){
int[] newArray = new int[this.array.length + 10];
System.arraycopy(this.array, 0, newArray, 0, this.array.length);
this.array = newArray;
}
this.array[index] = value;
this.index++;
}


//变小操作;空10个之后再删除长度;后面空着的通过下标抛出访问异常;
//移除元素
public void remove(int index){
if(index >= 0 && index < this.index){
System.arraycopy(this.array, index + 1, this.array, index, this.index - index - 1);
this.index--;
if((this.capibility() - this.size() > 10)
&& this.array.length > 10){
int[] newArray = new int[this.array.length - 10];
System.arraycopy(this.array, 0, newArray, 0, this.index);
this.array = newArray;
}
return;
}
throw new ArrayIndexOutOfBoundsException(index);
}

//修改某个位置的元素:
public void set(int index, int value){
if(index >= 0 && index < this.index){
this.array[index] = value;
}
throw new ArrayIndexOutOfBoundsException(index);
}

//获取某个位置的元素
public int get(int index){
if(index >= 0 && index < this.index){
return this.array[index];
}
throw new ArrayIndexOutOfBoundsException(index);
}

//得到放置了多少个元素
public int size(){
return this.index;
}
//得到数组容量是多少
public int capibility(){
return this.array.length;
}

3、JavaDoc:
生成方式:bin 中 javadoc命令; javac 编译 java运行命令;
右键工程,导出 java下的javaDOC 找到bin目录中的运行程序javaDoc---(设置导出访问修饰符,设置导出的文件地址 进入next next 该为-encoding utf-8)
在生成的文档中找到index,在网页上打开;
文档注释标记:
单行注释、多行注释、文档注释;
文档注释:/** 自动注释编写类;
*..... @author: 源代码的作者名称;
*..... @version:描述类的软件版本;
*/ @throws,与exception标记功能相同,被方法所用,列出抛出异常;
@see:创建一个“参见”XX条目的链接,如类、方法、变量等;
@since:描述成员存在或者改变时的版本号;
@value:显示常量静态字段的值;
@depracated:已经过期类成员,为保持兼容性依然存在;
/**
*功能介绍
*@pram 描述方法和形式参数
*@return描述方法的返回值
*/


static在面向对象中的特殊含义
表面是可以通过类名点出来 stu.方法;
1、静态成员变量,为该类的公用变量,在第一次使用时被初始化,并且该变量存放在数据区,只有一份;
2、在Static 声明的静态方法中,调用静态方法时,不会将对象的引用传递给他,方法不能访问非Static的成员;


flag/size/index:标记;


0 0
原创粉丝点击