java 实现线性表之顺序存储
2014-08-16 16:58
309 查看
package com.zhiru; public class MyArrayList { private int[]data; private int length; private int maxLength=100; MyArrayList(int size){ if(size<100){ length=0; data=new int[size]; } else{ System.out.println("size is too big"); } } //init the arraylist with the array a MyArrayList(int []a){ length=0; data=new int[maxLength]; if(a.length<maxLength){ for(int i=0;i<a.length;i++){ data[length++]=a[i]; } } else{ System.out.println("size is too big"); } } public int getLength(){ return length; } public void fill(int val){ data[length++]=val; } //set the value on the pos position. public void set(int pos,int val){ if(pos>0&&pos<length) data[pos-1]=val; } public int get(int pos){ if(pos>0&&pos<length){ return data[pos-1]; } return 0; } public void printArray(){ if(length>0){ for(int j=0;j<length;j++){ System.out.print(data[j]+" "); } System.out.print("\n"); } } public boolean deleteValByPos(int pos){ boolean o=false; if(pos>0&&pos<length+1){ for(int j=pos-1;j<length-1;j++){ //cong kaishi shanchu de wei zhi de hou yi wei kai shi //wang qian fu gai qianmian de zhi. data[j]=data[j+1]; } length--; o=true; } return o; } public boolean deleteValByVal(int val){ int m; for( m=0;m<length;m++){ if(data[m]==val) break; } // still use the method deleteValByPos to delete the val. return deleteValByPos(m+1); } public boolean insertToArray(int pos,int val){ boolean o=false; if(pos<1||pos>length){ System.err.print("wrong"); } else{ for(int j=length;j>=pos+1;j--){ data[j]=data[j-1]; } length++; data[pos]=val; o=true; } return o; } //if the data is empty public boolean isEmpty(){ return length==0; } //return the position of the value given //position=[0,length-1] public int Locate(int val){ int i=0; while(i<=length){ if(val==data[i]) break; else i++; } return i; } // decide whether the value is the data[] member. //exists true,else false. public boolean isExists(int val){ boolean o=false; int index=Locate(val); if(index>length) o=false; else o=true; return o; } public void jiaoJi(MyArrayList ml){ //i is the index of this object. //j is the index of ml's object int []x=new int[((this.length+1)<(ml.length+1)?(this.length+1):(ml.length+1))]; int i=0,index=0; while(i<=this.length){ if(ml.isExists(this.data[i])){ x[index++]=this.data[i]; } i++; } System.out.print("交集为:\n"); for(int ind=0;ind<index;ind++) System.out.print(x[ind]+" "); System.out.print("\n"); } /* * input:MyArrayList object * output:they are union. */ public void binJi(MyArrayList ml){ int mLen=ml.length+1; int j=0; while(j<mLen){ if(this.isExists(ml.data[j])==false){ this.insertToArray(this.length, ml.data[j]); } j++; } System.out.print("并集为:\n"); for(int i=0;i<this.length+1;i++){ System.out.print(this.data[i]+" "); } System.out.print("\n"); } }
package com.zhiru; public class TestArrayList { public static void main(String[] args) { // TODO Auto-generated method stub MyArrayList mal=new MyArrayList(20); MyArrayList mal1=new MyArrayList(20); int []a={1,2,5,6,7,8}; MyArrayList mal2=new MyArrayList(a); for(int i=0;i<10;i++){ mal.fill(i+1); mal1.fill(i+2); } mal.printArray(); mal1.printArray(); mal.jiaoJi(mal1); mal.binJi(mal1); mal2.printArray(); System.out.println(mal2.getLength()); mal2.insertToArray(1, 12); mal2.insertToArray(mal2.getLength(), 100); mal2.printArray(); mal2.deleteValByPos(1); mal2.printArray(); mal2.deleteValByPos(7); mal2.printArray(); // mal2.insertToArray(6, 20); } }
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10 11
交集为:
2 3 4 5 6 7 8 9 10 0
并集为:
1 2 3 4 5 6 7 8 9 10 11 0
1 2 5 6 7 8
6
1 12 2 5 6 7 8 100
12 2 5 6 7 8 100
12 2 5 6 7 8
相关文章推荐
- Java数据结构——线性表的顺序存储实现
- java实现线性表的顺序存储操作
- 线性表的顺序存储之java实现
- 线性表的顺序存储及JAVA实现
- 线性表的Java实现--顺序存储
- java实现数据结构中线性表的顺序存储
- 数据结构:线性表的顺序存储--Java实现
- java实现线性表的顺序存储
- 线性结构的顺序存储和链式存储的实现代码(一)
- 第3章 线性表的顺序存储c#实现---《大话数据结构》读书笔记
- 线性表--顺序实现方式 (JAVA)
- 数据结构:线性表(顺序存储)顺序表类(实现顺序表的创建,输出,插入,删除功能)
- 线性表的链式存储结构之单链表结点类的实现_Java
- 线性表的顺序存储实现及相关操作 C语言版
- 线性表java实现之顺序存储源码
- 线性表--顺序实现方式 (JAVA)
- 线性表的链式存储结构之单链表类的实现_Java
- 链表-顺序存储-Java实现
- 数组实现线性表的顺序存储
- 线性表的链式存储结构之单链表类的实现_Java