相遇'不要钱'

导航

java超级数组


//超级数组
public class SuperArray {
    public  Object obj[]=new Object[0];
    
    //增加一个
    public  void addArray(Object ob){
        if(getArrayLength()<=getLength()){
            obj[getArrayLength()]=ob;
            System.out.println("添加成功");
        }else{
            System.out.println("数组已满,不能存储");
        }
        
        
    }
    //删除一个,容量-1
    public void deleteOneArray(int index){
        if(index>getArrayLength()){
            System.out.println("下标"+index+"位置没有元素");
        }else{
            obj[index]=null;
            Object newObj[]=new Object[obj.length-1];
            int j=0;
            for (int i = 0; i < obj.length; i++) {
                if(obj[i]!=null){
                    newObj[j]=obj[i];
                    j++;
                }
            }
            obj=newObj;
            System.out.println("删除成功");
        }
    }
    //删除全部,容量=0
    public void deleteAllArray(){
//        for (int i = 0; i < obj.length; i++) {
//            obj[i]=null;
//        }
        Object newObj[]=new Object[0];
        obj=newObj;
        
        System.out.println("数组已清空");
    }
    //查询某一个
    public void seleteOneArray(int index){
        if(index<= getLength()){
            System.out.println("下标"+index+"的元素:"+obj[index]);
        }else if(index>getLength()){
            System.out.println("数组下标越界");
        }
        
    }
    //查询全部
    public void queryAllArray(){
        if(getArrayLength()<=0){
            System.out.println("当前数组没有任何元素");
        }else{
            System.out.println("当前数组元素:");
            for (int i = 0; i < obj.length; i++) {
                if(obj[i]!=null){
                    System.out.print(obj[i]+",");
                }
            }
            System.out.println();
        }
        
    }
    //当前数组的存储量
    public int getArrayLength(){
        int len=0;
        for (int i = 0; i < obj.length; i++) {
            if(obj[i]!=null){
                len++;
            }
        }
        return len;
    }
    //数组的总存储量
    public  int getLength(){
        return obj.length-1;
    }
    //更新单个元素
    public  void updateArray(int index,Object str){
        if(index<= getLength()){
            obj[index]=str;
            System.out.println("更新成功");
        }else{
            System.out.println("数组下标越界");
        }
    }
    
    //如果当前数组已满,则创建新的数组
    public void getNewArray(){
        Object newObj[]=new Object[obj.length+1];
        for (int i = 0; i < obj.length; i++) {
            newObj[i]=obj[i];
        }
        obj=newObj;
        System.out.println("新数组创建成功");
    }

}

 

、、、、。。。。


import java.util.Scanner;

//数组操作
public class SuperArrayView {
    public static SuperArray superA=new SuperArray();
    public static Scanner sc=new Scanner(System.in);
    
    public static void main(String []args){
        SuperArrayView superarray=new SuperArrayView();
        superarray.getProcess();
    }
    //主流程控制
    public void getProcess(){
        while(true){
            System.out.println("==============欢迎使用超级数组=================");
            System.out.println("1.查询单个元素\t2.查询所有元素");
            System.out.println("3.删除单个元素\t4.清空数组元素");
            System.out.println("5.增加单个元素\t6.更新单个元素");
            System.out.println("7.创建新的数组\t8.当前数组储量");
            System.out.println("9.当前数组容量\t10.退出");
            System.out.println("------------------------------------------");
            System.out.println("<输入其他数字,退出超级数组使用>");
            System.out.println("==========================================");
            System.out.println("请输入你的选择:");
            int n=sc.nextInt();
            switch(n){
                case 1:System.out.println("请输入你要查询元素的位置:");
                int index=sc.nextInt();
                superA.seleteOneArray(index);
                    break;
                case 2:superA.queryAllArray();
                    break;
                case 3:System.out.println("请输入你要删除元素的位置:");
                    int m=sc.nextInt();
                    superA.deleteOneArray(m);
                    break;
                case 4:superA.deleteAllArray();
                    break;
                case 5:System.out.println("请输入你要添加的元素:");
                String ob=sc.next();
                    superA.addArray(ob);
                    break;
                case 6:System.out.println("请输入你要更新元素的位置:");
                    int ind=sc.nextInt();
                    System.out.println("请输入更新的信息:");
                    String str=sc.next();
                    superA.updateArray(ind, str);
                    break;
                case 7:superA.getNewArray();
                    break;
                case 8:int sum=superA.getArrayLength();
                    System.out.println("当前数组储量:"+sum);
                    break;
                case 9:int count=superA.getLength();
                    System.out.println("当前数组容量:"+(count+1));
                    break;
                case 10:System.out.println("===退出使用===");
                    System.out.println("==============欢迎使用超级数组=================");
                    System.exit(0);
                    break;
                default:System.out.println("===操作继续===");
                    break;
                
                    
            }
        }
    }
}

posted on 2014-04-21 18:35  相遇'不要钱'  阅读(265)  评论(0)    收藏  举报