简体   繁体   中英

Java - swap arraylist index

I made a map by arraylist, and trying to swap the index(not the value) of arraylist.

For example, the swapRow(1,2) changes the position of row 1 and 2. This works at the first time, but if I do it again, an error comes out.(Same problem with the swapCol() part.)

Stuck in this problem and need help...

In addition, can anyone help me out making the swapValue() method. Changing the value is simple, though I also want to move the index, not the value.

import java.text.DecimalFormat;
import java.util.ArrayList;
public class IntMap {

    class Data {
        private int data_;

        public Data(int data)
        {

            setData(data);
        }

        public void setData(int data)
        {
            if(data<0)data=0;
            if(data>99)data=99;

            data_=data;

        }

        public int getData()
        {
            return data_;
        }

    }


    class Index {

        private int index_;

        public Index(int index)
        {
            setIndex(index);
        }

        public void setIndex(int index)
        {
            if(index < 0) index=0;

            index_=index;
        }

        public int getIndex()
        {
            return index_;
        }

    }



    private ArrayList<ArrayList<Data>> datas_;
    private ArrayList<Index> cols_;
    private ArrayList<Index> rows_;

    public IntMap(int rowCount, int colCount)
    {
        if(rowCount < 0) rowCount = 1;
        if(rowCount < 0) colCount = 1;

        cols_ = new ArrayList<Index>();
        rows_ = new ArrayList<Index>();
        datas_ = new ArrayList<ArrayList<Data>>();

        for(int i=0 ; i<rowCount ; ++i) rows_.add(new Index(i));
        for(int i=0 ; i<colCount ; ++i) cols_.add(new Index(i));

        for(int i=0 ; i< rowCount ; ++i)
        {
            ArrayList<Data> temp = new ArrayList<Data>();
            datas_.add(temp);

            for(int j=0 ; j<colCount ; ++j)
            {
                temp.add(new Data(0));
            }

        }

    }

    public int getValue(int row, int col)
    {
        int rIndex = getRowIndex(row);
        int cIndex = getColIndex(col);

        return datas_.get(rIndex).get(cIndex).getData();
    }

    public void setValue(int row, int col, int value)
    {
        int rIndex = getRowIndex(row);
        int cIndex = getColIndex(col);

        datas_.get(rIndex).get(cIndex).setData(value);

    }




    public void setRandomValue()
    {
        for(int row=0 ; row < datas_.size() ; ++row)
        {
            ArrayList<Data> temp = datas_.get(row);

            for(int col=0; col <temp.size() ; ++col)
            {
                Data data = temp.get(col);

                data.setData((int)(Math.random()*100));

            }
        }
    }

    public void log()
    {
        DecimalFormat df = new DecimalFormat("00");
        for(int row=0; row<rows_.size(); ++row)
        {
            for(int col=0; col<cols_.size(); ++col)
            {

                int value = getValue(row, col);
                System.out.print(df.format(value));

                if(col<cols_.size()-1)
                    System.out.print(", ");
            }

            System.out.println();
        }
        System.out.println();
    }

    private int getColIndex(int col)
    {
        if(col <0 || col>=cols_.size())
        {
            System.out.println("[error][IntMap][getColIndex] - col is"+col);
            return 0;
        }

        for(int i =0; i<cols_.size(); ++i)
        {
            if(cols_.get(i).getIndex()==col)
                return i;
        }

        System.out.println("[error][IntMap][getColIndex] - unknown error");
        return 0;

    }

    private int getRowIndex(int row)
    {
        if(row <0 || row>=rows_.size())
        {
            System.out.println("[error][IntMap][getRowIndex] - row is"+row);
            return 0;
        }

        for(int i =0; i<rows_.size(); ++i)
        {
            if(rows_.get(i).getIndex()==row)
                return i;
        }

        System.out.println("[error][IntMap][getRowIndex] - unknown error");
        return 0;

    }

    public void swapValue(int a, int b, int c, int d)
    {

        //

    }

    public void swapCol(int a, int b)
    {
        int col1=a;
        int col2=b;
        int t=a;

        cols_.get(col1).setIndex(col2);
        cols_.get(col2).setIndex(t);

    }

    public void swapRow(int a, int b)
    {   
        int t=a;

        rows_.get(a).setIndex(b);
        rows_.get(b).setIndex(t);

    }

    public static void main(String[] args)
    {
        System.out.println("start!");

        IntMap map =new IntMap(3,4);

        System.out.println("Init map 3x4");
        map.log();

        System.out.println("random map");
        map.setRandomValue();
        map.log();

        System.out.print("get map[0][1]= ");
        System.out.println(map.getValue(0,1));

        System.out.println("set map[2][1]=50");
        map.setValue(2, 1 ,50);
        map.log();

        System.out.println("swap(0,1 <-> 2,1)");
        map.swapValue(0,1,2,1);
        map.log();

        System.out.println("swapRow(0 <-> 2)");
        map.swapRow(0,2);
        map.log();

        System.out.println("swapRow(1 <-> 2)");
        map.swapRow(1,2);
        map.log();

        System.out.println("swapCol(0 <-> 3)");
        map.swapCol(0,3);
        map.log();


    }
}

I think you got confused the index of rows_ arrayList and your Index type. your swapRow(a,b) should check rows_ list and find out the element (Index Object) .getIndex = a and b, then swap the two Index elements, not just like what you did in your method. If you change the method into followings, it should work. you could do some error handling, eg what happend parameter a or b invalid. I just set themt to 0.

public void swapRow(int a, int b) {
        int ai = 0, bi = 0;
        for (final Index i : rows_) {
            if (i.getIndex() == a) {
                ai = rows_.indexOf(i);
            }
            if (i.getIndex() == b) {
                bi = rows_.indexOf(i);
            }
        }
        final Index aidx = rows_.get(ai);
        rows_.set(ai, rows_.get(bi));
        rows_.set(bi, aidx);

    }

and, i don't know your exact requirment. but from the design point of view, there might be room to improve.

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