会员
周边
众包
新闻
博问
闪存
赞助商
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
Horrison
低头做事,抬头做人,不要抱怨、不要攀比,相信自己!
首页
订阅
管理
自己写的ArrayList
package com.zc.hzc; public class MyAList { private int index;//数组长度 private Object[] ob;//数组 public MyAList(){ ob=new Object[10];//初始化数组长度为10 } public MyAList(int l){ ob=new Object[l];//有参数的构造函数 } /** * 扩充数组,翻一倍 */ public void expand(){ Object[]ob2=new Object[ob.length*2]; System.arraycopy(ob, 0, ob2, 0, ob.length); this.ob=ob2; } /** * 添加数据 * @param o */ public void add(Object o){ if(index==ob.length) { expand(); } ob[index]=o; index++; } /** * 将指定的元素插入此列表中的指定位置。 * @param pos * @param o */ public void add(int pos,Object o){ if(pos>index||pos<0){ System.out.println("插入位置:"+pos+", 元素为:"+o+"出错,没有此位置"); }else { if(index==ob.length) expand(); for(int i=index;i>pos;i--){ ob[i]=ob[i-1]; } ob[pos]=o; index++; } } /** * 移除此列表中指定位置上的元素。 * @param pos * @return */ public Object remove(int pos) { Object o=ob[pos]; for(int i=pos;i<index;i++){ ob[i]=ob[i+1]; } index--; return o; } /** * 返回此列表中的元素数。 */ /** * 搜索给定参数第一次出现的位置 */ public int indexOf(Object o){ for(int i=0;i<index;i++) if(ob[i].equals(o)) { return i; } return -1; } public int size(){ return index; } /** * 根据参数,返回相应的值 * @param i * @return */ public Object get(int i){ return ob[i]; } /* * 判断list是否为空 */ public Boolean isEmpty(){ return index==0; } /** * 主函数 * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub MyAList ml=new MyAList(); ml.add("hzc"); ml.add("hhx"); ml.add("kj"); ml.add(234); ml.add("123"); //ml.remove(2); for(int i=0;i<ml.size();i++){ System.out.println(ml.get(i)); } System.out.println(ml.size()); System.out.println(ml.indexOf("234")); } }
posted @
2011-07-09 20:12
Horrison
阅读(
293
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部