Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - swap arraylist index
    primarykey
    data
    text
    <p>I made a map by arraylist, and trying to swap the index(not the value) of arraylist.</p> <p>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.)</p> <p>Stuck in this problem and need help...</p> <p>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.</p> <pre><code>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&lt;0)data=0; if(data&gt;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 &lt; 0) index=0; index_=index; } public int getIndex() { return index_; } } private ArrayList&lt;ArrayList&lt;Data&gt;&gt; datas_; private ArrayList&lt;Index&gt; cols_; private ArrayList&lt;Index&gt; rows_; public IntMap(int rowCount, int colCount) { if(rowCount &lt; 0) rowCount = 1; if(rowCount &lt; 0) colCount = 1; cols_ = new ArrayList&lt;Index&gt;(); rows_ = new ArrayList&lt;Index&gt;(); datas_ = new ArrayList&lt;ArrayList&lt;Data&gt;&gt;(); for(int i=0 ; i&lt;rowCount ; ++i) rows_.add(new Index(i)); for(int i=0 ; i&lt;colCount ; ++i) cols_.add(new Index(i)); for(int i=0 ; i&lt; rowCount ; ++i) { ArrayList&lt;Data&gt; temp = new ArrayList&lt;Data&gt;(); datas_.add(temp); for(int j=0 ; j&lt;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 &lt; datas_.size() ; ++row) { ArrayList&lt;Data&gt; temp = datas_.get(row); for(int col=0; col &lt;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&lt;rows_.size(); ++row) { for(int col=0; col&lt;cols_.size(); ++col) { int value = getValue(row, col); System.out.print(df.format(value)); if(col&lt;cols_.size()-1) System.out.print(", "); } System.out.println(); } System.out.println(); } private int getColIndex(int col) { if(col &lt;0 || col&gt;=cols_.size()) { System.out.println("[error][IntMap][getColIndex] - col is"+col); return 0; } for(int i =0; i&lt;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 &lt;0 || row&gt;=rows_.size()) { System.out.println("[error][IntMap][getRowIndex] - row is"+row); return 0; } for(int i =0; i&lt;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 &lt;-&gt; 2,1)"); map.swapValue(0,1,2,1); map.log(); System.out.println("swapRow(0 &lt;-&gt; 2)"); map.swapRow(0,2); map.log(); System.out.println("swapRow(1 &lt;-&gt; 2)"); map.swapRow(1,2); map.log(); System.out.println("swapCol(0 &lt;-&gt; 3)"); map.swapCol(0,3); map.log(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload