自定义队列
1. 数组的优点与缺陷:
a>. 优点: 1. 方便管理同类型的数据
2. 搜索简单
b>. 缺陷 1. 数组长度固定,不能改变
2. 插入和删除数据不方便
2. 自定义数组的产生:对数组的改善,克服数组长度不能改变的缺陷,继承了数组搜索简单的优点
public class DifinitedQueue {
private int length=0;
int Array[]=new int [length];
public int getLength() {
return length;
}
//增加
public void add(int num) {
int []Array_new=new int[length+1];
for (int i=0;i<length;i++) {
Array_new[i]=Array[i];
}
Array_new[length]=num;
length++;
//返还给原来的数组,间接的改变了数组长度
Array=Array_new;
}
//插入
public void insert(int a,int index) {
int []Array_new=new int[length+1];
// 待插入位置之前所有数组全部复制到新数组
for (int i=0;i<index-1;i++) {
Array_new[i]=Array[i];
}
// 插入数组
Array_new[index-1]=a;
// 待插入位置之后所有数组全部复制到新数组
for(int i=index;i<length+1;i++) {
Array_new[i]=Array[i-1];
}
System.out.println("插入后的数组为:");
length++;
//返还给原来的数组,间接的改变了数组长度
Array=Array_new;
}
//删除
public void delete(int index) {
int []Array_new=new int[length-1];
int deletedNum;
// 待删除位置之前所有数组全部复制到新数组
for (int i=0;i<index-1;i++) {
Array_new[i]=Array[i];
}
deletedNum=Array[index-1];
// 待插入位置之后所有数组全部复制到新数组
for(int i=index;i<length;i++) {
Array_new[i-1]=Array[i];
}
//返还给原来的数组,间接的改变了数组长度
System.out.println("删除的数字是"+deletedNum);
Array=Array_new;
length--;
}
//查
public int get(int index) {
System.out.println("找到了");
return Array[index];
}
//改
public boolean update(int index,int num) {
try{
Array[index]=num;
System.out.println("修改成功");
return true;
}
catch(Exception e){
System.out.println("修改失败,索引下标越界!");
return false;
}
}
public static void main(String[] args) {
DifinitedQueue difinitedQueue = new DifinitedQueue();
}
}
泛型
定义:
1. 在程序编码中一些包含类型参数的类型,也就是说泛型的参数只可以代表类,不能代表个别对象。(这是当今较常见的定义)
2. 在程序编码中一些包含参数的类。其参数可以代表类或对象等等。(人们大多把这称作模板)不论使用哪个定义,泛型的参数在真正使 用泛型时都必须作出指明。
格式: e.g Map<K,V>
要点:
1>> 泛型都是引用类型 ,是堆对象
2>> 泛型类在创建对象时,可以赋任何引用类型
如:
8种基本类型: int long short double float char byte boolean
对应的引用类型: Integer Long Short Double Float Char Byte Boolean + String + 自定义引用类型
自定义队列和泛型的综合应用
public class VariableArray<T> {
private int length=0;
Object Array[]=new Object [length];
public int getLength() {
return length;
}
//增加
public void add(T num) {
Object []Array_new=new Object[length+1];
for (int i=0;i<length;i++) {
Array_new[i]=Array[i];
}
Array_new[length]=num;
length++;
//返还给原来的数组,间接的改变了数组长度
Array=Array_new;
}
//插入
public void insert(T a,int index) {
Object []Array_new=new Object[length+1];
// 待插入位置之前所有数组全部复制到新数组
for (int i=0;i<index-1;i++) {
Array_new[i]=Array[i];
}
// 插入数组
Array_new[index-1]=a;
// 待插入位置之后所有数组全部复制到新数组
for(int i=index;i<length+1;i++) {
Array_new[i]=Array[i-1];
}
System.out.println("插入后的数组为:");
length++;
//返还给原来的数组,间接的改变了数组长度
Array=Array_new;
}
//删除
public void delete(int index) {
Object []Array_new=new Object[length-1];
T deletedNum;
// 待删除位置之前所有数组全部复制到新数组
for (int i=0;i<index-1;i++) {
Array_new[i]=Array[i];
}
deletedNum=(T)Array[index-1];
// 待插入位置之后所有数组全部复制到新数组
for(int i=index;i<length;i++) {
Array_new[i-1]=Array[i];
}
//返还给原来的数组,间接的改变了数组长度
System.out.println("删除的数字是"+deletedNum);
Array=Array_new;
length--;
}
//查
public T get(int index) {
System.out.println("找到了");
return (T)Array[index];
}
//改
public boolean update(int index,T num) {
try{
Array[index]=num;
System.out.println("修改成功");
return true;
}
catch(Exception e){
System.out.println("修改失败,索引下标越界!");
return false;
}
}
}
Shape类
public abstract class Shape {
int x1,y1,x2,y2; // 鼠标拖动起点和终点的位置
float strock; //画笔的宽度
Color currentColor; //设置当前画笔的颜色
public Shape(int x1, int y1, int x2, int y2, Color currentColor, float strock) {
super();
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.currentColor = currentColor;
this.strock = strock;
}
public abstract void draw(Graphics2D g);
}
具体应用泛型如:
ArrayList<Shape> array1=new ArrayList<Shape>();