首页 > 代码库 > 数据结构线性表顺序结构的实现
数据结构线性表顺序结构的实现
package com.he.list; import java.util.Arrays; import org.w3c.dom.ls.LSException; class ArrayList { private int length;// the list's length private int[] store;// store the data // initialize an empty list public ArrayList initList(String name) { return new ArrayList(); } public ArrayList() { store = new int[0]; } // get the list's length public int getLength() { return store.length; } // add data into the list public void add(int data) { if (store.length == 0) { store = Arrays.copyOf(store, length + 1); store[0] = data; length++; } else { store = upSize(); store[store.length - 1] = data; } } public boolean isEmpty() { return length == 0; } public int[] upSize() { store = Arrays.copyOf(store, ++length); return store; } // index the list public int get(int i) { return store[i]; } // find if the list contains data and return the index public int contains(int data) { for (int i = 0; i < length; i++) if (store[i] == data) return i; return 0; } // delete a data in the list and the data's index is i public boolean delete(int i) { for (int t = i; t < length; t++) { store[t - 1] = store[t]; } store = Arrays.copyOf(store, length - 1); return true; } } public class MyArrayList { public static void main(String[] args) { ArrayList list = new ArrayList(); for (int i = 0; i <= 10; i++) list.add(i); for (int i = 0; i < list.getLength(); i++) System.out.println(list.get(i)); System.out.println(list.isEmpty()); System.out.println(list.contains(1)); System.out.println(list.contains(10)); System.out.println(list.contains(111)); list.delete(9); System.out.println(list.getLength()); for (int i = 0; i < list.getLength(); i++) System.out.println(list.get(i)); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。