码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构线性表顺序结构的实现

时间:2014-06-14 11:16:05      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:class   blog   code   java   com   get   

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));
	}
}

数据结构线性表顺序结构的实现,布布扣,bubuko.com

数据结构线性表顺序结构的实现

标签:class   blog   code   java   com   get   

原文地址:http://blog.csdn.net/superstonne/article/details/30547821

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!