我这几天碰到的面试题,大家有空帮我写一个代码。谢谢了

解决方案 »

  1.   

    是的,(他还注备了:不能用现成的JAVA API,要自己写方法)
      

  2.   

    难道是要这样的?
    int[] a = {1,2,3};
    public void add(int a) {
        int[] b = new int[a.length+1];
        System.arraycopy(a,0,b,0,a.length);
        b[a.length] = 4;
    }
      

  3.   

    你自己看看ArrayList的代码就知道怎么实现了
      

  4.   

    用ArrayList aa=new ArrayList();
    看看它里面的方法就可以了.
      

  5.   

    fast_time:老兄:能不能详细指点一下。写一下代码。谢谢!
      

  6.   

    package test;public class Arrays {

    int size=10;

    Object[] objs;

    public Arrays(){
    objs=new Object[] {1,2,3,4,5,6,7,8,9,10};
    }

    public void delete(int at){
    Object[] newobjs=new Object[size-1];
    for(int i=0,j=0;i<size;i++,j++){
    if(i==at){
    j--;
    continue;
    }
    newobjs[j]=objs[i];
    }
    objs=newobjs;
    size--;
    }

    public void print(){
    for(int i=0;i<size;i++){
    System.out.println(""+objs[i]);
    }
    }

    public void add(Object value,int at){
    Object[] newobjs=new Object[size+1];
    int flag=0;
    for(int i=0,j=0;i<size;i++,j++){
    if(i==at&&flag==0){
    //System.out.println(i+" "+j);
    newobjs[j]=value;
    i--;
    flag=1;
    continue;
    }

    newobjs[j]=objs[i];
    }
    objs=newobjs;
    size++;
    }


    public void modify(Object value,int at){
    for(int i=0;i<size;i++){
    if(i==at){
    objs[i]=value;
    }
    }
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    Arrays arr=new Arrays();
    arr.print();
    arr.delete(2);
    arr.print();
    arr.add(5, 2);
    arr.print();
    arr.modify(100, 1);
    arr.print();
    }}
      

  7.   

    private transient Object elementData[];
        
        private int size;
        
        public void ensureCapacity(int minCapacity) {
         int oldCapacity = elementData.length;
         if (minCapacity > oldCapacity) {
             Object oldData[] = elementData;
             int newCapacity = (oldCapacity * 3)/2 + 1;
                 if (newCapacity < minCapacity)
         newCapacity = minCapacity;
             elementData = new Object[newCapacity];
             System.arraycopy(oldData, 0, elementData, 0, size);
         }
        }
        
        public boolean add(Object o) {
         ensureCapacity(size + 1);  // Increments modCount!!
         elementData[size++] = o;
         return true;
        }    public void add(int index, Object element) {
         if (index > size || index < 0)
             throw new IndexOutOfBoundsException(
         "Index: "+index+", Size: "+size);     ensureCapacity(size+1);  // Increments modCount!!
         System.arraycopy(elementData, index, elementData, index + 1,
          size - index);
         elementData[index] = element;
         size++;
        }
        
        public Object remove(int index) {
         RangeCheck(index);     Object oldValue = elementData[index];     int numMoved = size - index - 1;
         if (numMoved > 0)
             System.arraycopy(elementData, index+1, elementData, index,
              numMoved);
         elementData[--size] = null; // Let gc do its work     return oldValue;
        }
        
        private void RangeCheck(int index) {
         if (index >= size)
             throw new IndexOutOfBoundsException(
         "Index: "+index+", Size: "+size);
        }
      

  8.   

    这种公司购无聊,什么SBBT人用这种问题面试
      

  9.   

    写了一个,完全没用到java API。
    直接copy 运行 就出结果。
    -------------------------------------------------------------------------------C:\jdk5\bin>java MyArrayApp
    display the array which all of its elems are created by randomData :
    {3,7,4,5,7,4,0,1,5,3,0,3,5,5,4,7,8,0,6,1,1,7,6,8,4,4,9,1,5,8}
    delete first elem :
    {7,4,5,7,4,0,1,5,3,0,3,5,5,4,7,8,0,6,1,1,7,6,8,4,4,9,1,5,8}
    delete elem 9 :
    {7,4,5,7,4,0,1,5,3,0,3,5,5,4,7,8,0,6,1,1,7,6,8,4,4,1,5,8}
    delete allElems 6 :
    {7,4,5,7,4,0,1,5,3,0,3,5,5,4,7,8,0,1,1,7,8,4,4,1,5,8}
    add elem 99 at the beginning of the array :
    {99,7,4,5,7,4,0,1,5,3,0,3,5,5,4,7,8,0,1,1,7,8,4,4,1,5,8}
    replace the first elem with elem 999 :
    {999,7,4,5,7,4,0,1,5,3,0,3,5,5,4,7,8,0,1,1,7,8,4,4,1,5,8}C:\jdk5\bin>
    --------------------算 法 如 下------------------------------------------------
    class MyArray{
      private Object[] myarr;
      private int maxSize;
      private int nElems;  public MyArray(int size){
        maxSize=size;
        myarr=new Object[maxSize];
        nElems=0;
      }  public void display(){
       System.out.print("{");
        for(int i=0;i<nElems-1;i++)
          System.out.print(myarr[i]+",");
        System.out.print(myarr[nElems-1]+"}");    
        System.out.println();
      }  public void insert(Object value){
        myarr[nElems++]=value;
      }  public boolean isEmpty(){
        return nElems==0;
      }
      
      public boolean isFull(){
        return nElems==maxSize;
      }  public int size(){
        return nElems;
      }  public boolean delete(int index){
        if(index<0 || index>nElems){
          System.out.println("index out of bounds");
          return false;
        }
        else{
           Object temp=myarr[index];
           for(int i=index;i<nElems-1;i++)
            myarr[i]=myarr[i+1];
          nElems--;
        }
        return true;
      }  public boolean delete2(Object key){
        int i;    
        for(i=0;i<nElems;i++){
          if(myarr[i]==key)
            break;
          else if(i==(nElems-1) &&myarr[i] !=key)
            return false;
        }
        for(int j=i;j<nElems;j++)
          myarr[j]=myarr[j+1];
        nElems--;
        return true;
      }  public void deleteAll(Object key){
        int i,j;
        for(i=0;i<nElems;i++){
          if(myarr[i]==key){
            for(j=i;j<nElems;j++)
              myarr[j]=myarr[j+1];
            nElems--;
          }
        }
      }  public void addItem(int index,Object value){
        if(isFull())
          System.out.println("the array is full");
        else{
          for(int k=nElems-1;k>=index;k--)
            myarr[k+1]=myarr[k];
          myarr[index]=value;
          nElems++;
        }
      }  public Object replace(int index,Object newValue){
        Object oldValue=myarr[index];
        myarr[index]=newValue;
        return oldValue;
      }
    }---------------------------下 面 的 可 自 已 写-------------------------------------
    public class MyArrayApp{
      public static void main(String args[]){
        int arrSize=30;
        MyArray ma=new MyArray(arrSize);
        for(int i=0;i<arrSize;i++)
          ma.insert((int)(Math.random()*10));
        System.out.println("display the array which all of its elems are created by randomData :");
        ma.display();
        ma.delete(0);
        System.out.println("delete first elem :");
        ma.display();
        System.out.println("delete elem 9 :");
        ma.delete2(9);
        ma.display();
        System.out.println("delete allElems 6 :");
        ma.deleteAll(6);
        ma.display();
        System.out.println("add elem 99 at the beginning of the array :");
        ma.addItem(0,99);
        ma.display();
        System.out.println("replace the first elem with elem 999 :");
        ma.replace(0,999);
        ma.display();
      }
    }