我最近在学数据结构,  因为是菜鸟,所以有很多不懂!~
书上是以最通用的数据结构, 数组来开始的!~但是并没有提供实现数组的java程序,  我现在看到了链表,  可以用链表来实现数组的简单程序,
如果不用链表, 用简单的java程序, 如何来实现数组?.  用OO的思想,力求简单,          
请高手给予代码,  谢谢::;;;::::::::
如果还可以的话,  麻烦再在此自己创建的数组上写出一段 向量 代码!!

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【bxdg42578670】截止到2008-07-24 15:57:34的历史汇总数据(不包括此帖):
    发帖的总数量:26                       发帖的总分数:430                      每贴平均分数:16                       
    回帖的总数量:57                       得分贴总数量:17                       回帖的得分率:29%                      
    结贴的总数量:24                       结贴的总分数:420                      
    无满意结贴数:7                        无满意结贴分:180                      
    未结的帖子数:2                        未结的总分数:10                       
    结贴的百分比:92.31 %               结分的百分比:97.67 %                  
    无满意结贴率:29.17 %               无满意结分率:42.86 %                  
    值得尊敬
      

  2.   

    java本来就有数组,类型+[]就可以声明数组
      

  3.   

    在别的有些语言里 ,  数组是 基础类型,  
    在java里 , 数组 是引用,      我说的是 OO思想, 自己实现数组 和 它的一些操作,不用数组, 而编写出来的 一个可以实现数组一样操作的 程序!

      

  4.   

    就向量的例子最短,直接在这给吧。
    import java.util.Vector;public class StringVectorExample {//向量的例子
        public static void main(String[] args) {
            Vector stringList=new Vector();
            System.out.println("Line2:Empty:stringList?:  "+stringList.isEmpty());
            System.out.println("Line3:Size of stringList?:  "+stringList.size());
            System.out.println("");
            stringList.addElement("Spring");
            stringList.addElement("Summer");
            stringList.addElement("Fall");
            stringList.addElement("Winter");
            stringList.addElement("Sunny");
            System.out.println("Line10:***After adding elements to stringList****");
            System.out.println("Line11:Empty:stringList?:  "+stringList.isEmpty());
            System.out.println("Line12:Size of stringList?:  "+stringList.size());
            System.out.println("Line13:stringList:   "+stringList);
            System.out.println("Line14:stringList contains Fall?:  "+stringList.contains("Fall"));
            System.out.println("");
            stringList.insertElementAt("cool", 1);
            System.out.println("Line18:stringList:  "+stringList);
            System.out.println("");
            
        }
    }
      

  5.   

    其他的真的很长的,算了,直接在这给大家看吧。
    package arraylist;public abstract class DataElement {//定义基本的数据类型,有相等,比较,复制,获得副本等基本方法
    //我就写了int型和string型的数据类型,double,float的你自己可以补全就是了
        public abstract boolean equals(DataElement otherElement);    public abstract int compareTO(DataElement otherElement);    public abstract void makeCopy(DataElement otherElement);    public abstract DataElement getCopy();
    }
    package arraylist;public class IntElement extends DataElement {//int的类型补全了DataElement的抽象方法    protected int num;    public IntElement() {
            num = 0;
        }    public IntElement(int x) {
            num = x;
        }    public IntElement(IntElement otherElement) {
            num = otherElement.num;
        }    public void setNum(int x) {
            num = x;
        }    public int getNum() {
            return num;
        }    @Override
        public boolean equals(DataElement otherElement) {
            IntElement temp = (IntElement) otherElement;
            return (num == temp.num);
        }    @Override
        public int compareTO(DataElement otherElement) {
            IntElement temp = (IntElement) otherElement;
            return (num - temp.num);
        }    @Override
        public void makeCopy(DataElement otherElement) {
            IntElement temp = (IntElement) otherElement;
            num = temp.num;
        }    @Override
        public DataElement getCopy() {
            IntElement temp = new IntElement(num);
            return temp;
        }    @Override
        public String toString() {
            return String.valueOf(num);
        }
    }
      

  6.   

    package arraylist;public class StringElement extends DataElement {//string的类型补全了DataElement的抽象方法    private String str;    public StringElement() {
            str = null;
        }    public StringElement(String x) {
            str = x;
        }    public StringElement(StringElement otherElement) {
            str = otherElement.str;
        }    public void setString(String x) {
            this.str = x;
        }    @Override
        public boolean equals(DataElement otherElement) {
            StringElement temp = (StringElement) otherElement;
            return (str.compareTo(temp.str) == 0);
        }    @Override
        public int compareTO(DataElement otherElement) {
            StringElement temp = (StringElement) otherElement;
            return (str.compareTo(temp.str));
        }    @Override
        public void makeCopy(DataElement otherElement) {
            StringElement temp = (StringElement) otherElement;
            str = new String(temp.str);
        }    @Override
        public DataElement getCopy() {
            StringElement temp = new StringElement(str);
            return temp;
        }    public String toString() {
            return str;
        }
    }package arraylist;public abstract class ArrayListClass {//基于数组的线性表    protected int length;//现在表的长度
        protected int maxSize;//最大长度
        protected DataElement[] list;//装内容的表    public ArrayListClass() {
            maxSize = 100;
            length = 0;
            list = new DataElement[maxSize];
        }    public ArrayListClass(int size) {//定义最大长度
            if (size <= 0) {
                System.err.println("The array size must be positive.Creating an array of size 100.");
                maxSize = 100;
            } else {
                maxSize = size;
            }
            length = 0;
            list = new DataElement[maxSize];
        }    public ArrayListClass(ArrayListClass otherList) {
            maxSize = otherList.maxSize;
            length = otherList.length;
            list = new DataElement[maxSize];
            for (int i = 0; i < length; i++) {
                list[i] = otherList.list[i].getCopy();
            }
        }
    //上面的都是初始化的
        public boolean isEmpty() {
            return (length == 0);
        }    public boolean isFull() {
            return (length == maxSize);
        }    public int listSize() {
            return length;
        }    public int maxListSize() {
            return maxSize;
        }    public void print() {
            for (int i = 0; i < length; i++) {
                System.out.print(list[i] + " ");
            }
            System.out.println();
        }    public boolean isItemAtEqual(int loction, DataElement item) {//在指定位置是否相等
            return (list[loction].equals(item));
        }    public void inserAt(int location, DataElement insertItem) {//在指定位置插入
            if (location < 0 || location >= maxSize) {
                System.err.println("The position of the item to be inserted is out of range");
            } else if (length >= maxSize) {
                System.err.println("Cannot insert in a full list");
            } else {
                for (int i = length; i > location; i--) {
                    list[i] = list[i - 1];
                }
                list[location] = insertItem.getCopy();
                length++;
            }
        }    public void insertEnd(DataElement insertItem) {//在末尾插入
            if (length >= maxSize) {
                System.err.println("Cannot insert in a full list.");
            } else {
                list[length] = insertItem.getCopy();
                length++;
            }
        }    public void removeAt(int location) {//在指定位置删除
            if (location < 0 || location >= length) {
                System.err.println("The location of the item to be removed is out of range");
            } else {
                for (int i = location; i < length - 1; i++) {
                    list[i] = list[i + 1];
                }
                list[length - 1] = null;
                length--;
            }
        }    public DataElement retrieveAt(int location) {//在指定位置的数据,也就是内容
            if (location < 0 || location > length) {
                System.err.println("The location of the item to be retrieved is out of range");
                return null;
            } else {
                return list[location].getCopy();
            }
        }    public void replaceAt(int location, DataElement repItem) {//在指定位置替换
            if (location < 0 || location > length) {
                System.err.println("The location of the item to be replaced is out of range");
            } else {
                list[location].makeCopy(repItem);
            }
        }    public void clearList() {//清空表
            for (int i = 0; i < length; i++) {
                list[i] = null;
            }
            length = 0;
            System.gc();//清除垃圾
        }    public abstract int seqSearch(DataElement searchItem);    public abstract void insert(DataElement insertItem);    public abstract void remove(DataElement removeItem);    public void copyList(ArrayListClass otherList) {//复制另一个线性表
            if (this != otherList) {
                for (int i = 0; i < list.length; i++) {
                    list[i] = null;
                }
                System.gc();
                maxSize = otherList.maxSize;
                length = otherList.length;
                list = new DataElement[maxSize];
                for (int i = 0; i < length; i++) {
                    list[i] = otherList.list[i].getCopy();
                }
            }
        }
    }
      

  7.   

    package arraylist;public class UnorderedArrayList extends ArrayListClass {//无序线性表,其实也就在原来的 那个基础上补充了几个方法    public UnorderedArrayList() {
            super();
        }    public UnorderedArrayList(int size) {
            super(size);
        }    public UnorderedArrayList(UnorderedArrayList otherList) {
            super(otherList);
        }    @Override
        public int seqSearch(DataElement searchItem) {//在表中是否有与给的数据相同的,有的话返回下标,没有则返回-1
            int loc;
            boolean found = false;
            for (loc = 0; loc < length; loc++) {
                if (list[loc].equals(searchItem)) {
                    found = true;
                    break;
                }
            }
            if (found) {
                return loc;
            } else {
                return -1;
            }
        }//顺序查找    @Override
        public void insert(DataElement insertItem) {//插入数据
            int loc;
            if (length == 0) {
                list[length++] = insertItem;
            } else if (length == maxSize) {
                System.err.println("Cannot insert in a full list.");
            } else {
                loc = seqSearch(insertItem);
                if (loc == -1) {
                    list[length++] = insertItem.getCopy();
                } else {
                    System.err.println("The item to be inserted is already in the list." 
                            + "NO duplicates are allowed");
                }
            }
        }    @Override
        public void remove(DataElement removeItem) {//删除数据
            int loc;
            if (length == 0) {
                System.err.println("Cannot delete from an empty list");
            } else {
                loc = seqSearch(removeItem);
                if (loc != -1) {
                    removeAt(loc);
                } else {
                    System.out.println("The item to be deleted is not in the list.");
                }
            }
        }
    }package arraylist;import java.io.*;
    import java.util.*;public class Example {//线性表的例子
        static BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in));
        public static void main(String[] args) throws IOException{
            UnorderedArrayList intList=new UnorderedArrayList(50);
            UnorderedArrayList temp=new UnorderedArrayList();
            IntElement num=new IntElement();
            int counter;
            int position;
            StringTokenizer tokenizer;
            System.out.println("Line7:Processing the integer list");
            System.out.print("Line8:Enter 8 integers:");
            System.out.flush();
            tokenizer=new StringTokenizer(keyboard.readLine());
            for(counter=0;counter<8;counter++){
                num.setNum(Integer.parseInt(tokenizer.nextToken()));
                intList.insert(num.getCopy());//应该是num.getCopy(),给的是num的副本。要不就错误
            }
            temp.copyList(intList);
            System.out.println();
            System.out.println("Line 16:The list you entered is:  ");
            intList.print();
            System.out.println();
            System.out.println("Line 19:Enter the num to be deleted:  ");
            System.out.flush();
            num.setNum(Integer.parseInt(keyboard.readLine()));
            System.out.println();
            intList.remove(num);
            System.out.println("Line 24:After removing:  ");
            intList.print();
            System.out.println();
            System.out.println("Line 27:Enter the position of the num to be deleted: ");
            System.out.flush();
            position=Integer.parseInt(keyboard.readLine());
            System.out.println();
            intList.removeAt(position-1);
            System.out.println("Line 32:After removing:  ");
            intList.print();
            System.out.println();
            System.out.println("Line 35:Enter the search item: ");
            System.out.flush();
            num.setNum(Integer.parseInt(keyboard.readLine()));
            System.out.println();
            if (intList.seqSearch(num)!=-1) {
                System.out.println("Line 40:Item found in the list");
            } else {
                System.out.println("Line 42:Item not found");
            }
            System.out.print("Line 43:The temp is: ");
            temp.print();
            System.out.println();
        }
    }
      

  8.   

    代码太长了, 不符合我的要求!To: 9楼:我唯一想不通的是,用什么样的一个方法, 能确定数组里的数据类型;public void type(这里传进去一个什么的参数呢?)
      

  9.   

    靠  我学的时候都是用STRUCT 来做,当然那是用C    
    struct 
    {
      int a ;
      char b 
      char  *P
    } *con通过这种方式来连咯! 上方的那个写得好多咯! 看得都累了咯! 很强大,很多! 
      

  10.   

    public Class type(Object obj)
    {
        return obj.getClass();
    }
      

  11.   

    在每个写的数据类型里写个方法public String type(){return typename/*typename在IntElement里面是"int",在StringElement里面是"String"*/}想知道类型就怎么返回就是了。
    例如,DataElement a=new IntElement(4);要得到返回类型就是a.type();就好了。
    要数据类型一般没什么用吧,当然类似数据库的设计就要用到了。
      

  12.   

    不用链表,仅用基本语法、基本控制流,在 Java 中根本没办法实现数组!数组是在内存中开僻一块连续的空间,Java 根本不能让你对系统的内存空间进行操作,
    连最基本的指针操作都是不对外开放的!
      

  13.   



    那 按你的解释, 数组在java中的实现, 根本没有遵循java的机制吗》《
      

  14.   

    没搞明白用java写个这东西有什么用。
      

  15.   


    因为没有学过C 语言, 只学过java, 想明白数组这种数据结构~! 才提出这样的问题!
    最近在学数据结构, 发现用java思想, 很难往下理解!·
    过来人给个经验!~~
    我觉得java应该是可以写出数组的 , 因为java将它当成了一个对象的引用, 能实现和对象同样的功能!所以我觉得应该可以实现!~~

      

  16.   

    用java学数据结构算是比较轻松的。
      

  17.   

    作为我这样一个 java的 入门者而言,  再去看看C语言, 学习数据结构, 会不会影响到java!

      

  18.   

    java无法实现数组, 因为 [] 运算符没法重载. 当然类似的功能可以实现. 但是和真正的数组相去太远.用C++或C#都可以伪实现.
    真正的底层数组是一块连续的内存区, 数组名和数组0元素的地址有对应关系, 当访问元素[3]的时候,其实就是从首地址偏移3个单位.
    具体移动的个数取决于数组里的数据类型, 比如整数就要移动4个字节, char型移动1个字节.
    其实就是指针的加减运算.当然如果想再底层实现的话, 那得考虑如何将几个字节的连续数据转换成相应类型的数据. 因为最基本的就是字节了.
    比如4个字节的数据 00000001 00000010 00000011 00000100. 用四个字节存储, 把这个数据转换成为整数的时候就是2^24+2^17+2^9+2^8+2^2.如果要转换成char
    就是4个字符 分别是 '\1' '\2' '\3' '\4'. 都是不可见字符 , 等等.... 可以看一下编码方面的书