简体   繁体   中英

Java Q. How can I do insert&delete&print in Two Dimensional Array

Java Q. How can I do insert & delete & print in Two Dimensional Array...

How I can do it in Two Dimensional like this private int [] [] arr = new int [3] [3];

If I do it in 1D array like this

public class List {


    private int [] arr=new int[1000];
    private int size=0;


    public void add(int e){
        arr[size]=e;
        size++;

    }

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

        System.out.println();
    }

    public void insert(int e,int pos){
        if (pos<=size){
        size++;
        for(int i=size;i>pos;i--)
            arr[i]=arr[i-1];

        arr[pos]=e;
        }
        else System.out.print("unbounded ..");      
    }



    public void delete(int e){

        int pos=locate(e);
        if (pos!=-1){
          for(int i=pos;i<size-1;i++)
              arr[i]=arr[i+1];

          size--;
        }

    }


+++++++++++++++++++++++


                List list=new List();
        list.add(10);
        list.add(2);
        list.add(40);

        list.delete(103);

        list.dispaly();

You can use nested "for" loops to print the array. The first for handling the row number, and the second handling the column number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM