簡體   English   中英

Java-交換arraylist索引

[英]Java - swap arraylist index

我通過arraylist創建了一個映射,並嘗試交換arraylist的索引(而不是值)。

例如,swapRow(1,2)更改第1行和第2行的位置。這在第一次起作用,但是如果我再次執行此操作,則會出現錯誤。(swapCol()部分存在相同問題。)

卡在這個問題上,需要幫助...

另外,任何人都可以幫助我制作swapValue()方法。 更改值很簡單,盡管我也想移動索引而不是值。

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();


    }
}

我認為您對rows_ arrayList的索引和Index類型感到困惑。 您的swapRow(a,b)應該檢查rows_ list並找出元素(索引對象).getIndex = a和b,然后交換兩個Index元素,而不僅僅是您在方法中所做的。 如果將方法更改為以下方法,則應可以使用。 您可以進行一些錯誤處理,例如參數a或b無效。 我只是將themt設置為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);

    }

而且,我不知道您的確切要求。 但從設計的角度來看,仍有改進的空間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM