zoukankan      html  css  js  c++  java
  • ArrayList类

    JAVA中的集合分为有序可重复的List接口和无序不可重复的set接口

    实现了list接口的常用类:ArrayList类和LinkedList类。

    ArrayList类底层的结构是动态数组,擅长查询。

    LinkedList底层结构是链表,擅长插入删除。

    ArrayList类的主要方法:

    ArrayList的用法(转载)

    写一个简单的模仿ArrayList类的简单类:

    实现add(),remove(),get(),set()方法。

     1 package Collection;
     2 
     3 public class MyArrayList {
     4     private Object[] objs;
     5     private int size;
     6     private int lenth;
     7     
     8     public Object[] getObjs() {
     9         return objs;
    10     }
    11     public int getSize() {
    12         return size;
    13     }
    14     public int getLenth() {
    15         return lenth;
    16     }
    17     public void setLenth(int lenth) {
    18         if(lenth < 0){
    19             try {
    20                 throw new Exception();
    21             } catch (Exception e) {
    22                 // TODO Auto-generated catch block
    23                 e.printStackTrace();
    24             }
    25         }
    26         this.lenth = lenth;
    27     }
    28     public MyArrayList(){
    29         this.lenth = 10;
    30         this.objs = new Object[lenth];
    31     }
    32     public MyArrayList(int lenth) {
    33         if(lenth < 0){
    34             try {
    35                 throw new Exception();
    36             } catch (Exception e) {
    37                 // TODO Auto-generated catch block
    38                 e.printStackTrace();
    39             }
    40         }
    41         this.objs = new Object[lenth];
    42     }
    43     public boolean add(Object obj){
    44         Object[] oldObjectList = this.objs;
    45         if(this.size == this.objs.length){
    46             this.objs = new Object[2 * this.objs.length + 1];
    47         }
    48         System.arraycopy(oldObjectList, 0, this.objs, 0, this.size);
    49         this.objs[this.size++] = obj;
    50         return true;
    51     }
    52     public Object remove(int index){
    53         Object obj = get(index);
    54         Object[] oldObjectList = this.objs;
    55         this.objs = new Object[this.size];
    56         for(int i = 0; i < this.size; i++){
    57             if(i < index){
    58                 this.objs[i] = oldObjectList[i];
    59             }else if(i >= index){
    60                 this.objs[i] = oldObjectList[i + 1];
    61             }
    62         }    
    63         this.size--;
    64         return obj;
    65     }
    66     public Object get(int index){
    67         checkBound(index);
    68         return this.objs[index];
    69     }
    70     public Object set(int index, Object obj){
    71         Object old = this.objs[index];
    72         this.objs[index]  = obj;
    73         return old;
    74     }
    75     private void checkBound(int index){
    76         if(index < 0 || index > size){
    77             try {
    78                 throw new Exception();
    79             } catch (Exception e) {
    80                 // TODO Auto-generated catch block
    81                 e.printStackTrace();
    82             }
    83         }
    84     }
    85 }

    检测类:

     1 package Collection;
     2 
     3 public class test {
     4     public static void main(String args[]){
     5         MyArrayList mal = new MyArrayList();
     6         for(int i = 0; i < 9; i++){
     7             mal.add(i);
     8         }
     9         System.out.println(mal.getSize());
    10         for(int i = 0; i < 9; i++){
    11             System.out.print(mal.get(i) + " ");
    12         }
    13         System.out.println();
    14         mal.remove(1);
    15         System.out.println(mal.getSize());
    16         for(int i = 0; i < 8; i++){
    17             System.out.print(mal.get(i) + " ");
    18         }
    19         mal.set(1, 10);
    20         System.out.println();
    21         for(int i = 0; i < 8; i++){
    22             System.out.print(mal.get(i) + " ");
    23         }
    24     }
    25 }

    结果:

    比较简单,没写注释,凑活着看吧。

  • 相关阅读:
    并行编程——OPENMP
    并行编程——MPI/OPENMP混合编程
    C#中窗体间传递数据的几种方法
    开发人员一定要加入收藏夹的网站
    Web网站中从Sybase数据库读取的中文显示为乱码的解决方法
    数据空间和日志空间分离的操作方法
    双机集群中的数据库配置同步
    删除已损坏库方法
    RDLC报表中如何实现行交替颜色
    安装Sybase时安装界面为乱码的解决方法
  • 原文地址:https://www.cnblogs.com/cxy2016/p/7122032.html
Copyright © 2011-2022 走看看