大概把我的问题描述一下
我在做一个二维链表,每个节点都是一个link的对象
我给这个链表设置了一个游标currentCell
当我想在单元格(2, 2)设值22时,currentCell的坐标明明是(2, 2)但是输出结果是第2列(从0开始算起)全部都是22了下面是代码,只有一点简单的注释,很抱歉
class link
{
public int data;
public link goRight;
public link goDown;

public link(int d)
{
data = d;
goRight = null;
goDown = null;
}

public link()
{
data = 0;
goRight = null;
goDown = null;
}
}class linkList
{
private link head;
private link tail;
private link flag;
private int position;
public static final int ROW = 0;
public static final int COLUMN = 1; 

public linkList(int len, int mode)
{
link newlink = new link();
head = newlink;
tail = newlink;
flag = newlink;
position = 0;
if(mode == ROW)
{
for(int i=1; i<len; i++)
{
appendAtRight();
}
}
else if(mode == COLUMN)
{
for(int i=1; i<len; i++)
{
appendAtBottom();
}
}
}

public void appendAtRight()
{
link newlink = new link();
tail.goRight = newlink;
tail = newlink;
}

public void appendAtBottom()
{
link newlink = new link();
tail.goDown = newlink;
tail = newlink;
}

public link getElemAt(int n, int mode)
{
if(n==position)
return flag;
else if(n>position)
{
while(position!=n)
{
position++;
if(mode == ROW)
flag = flag.goRight;
else if(mode == COLUMN)
flag = flag.goDown;
}
return flag;
}
else
{
flag = head;
position = 0;
return getElemAt(n, mode);
}

}
}class linkListTable
{
private int width;
private int height;
private link tableTag; //二维链表的"入口"
private link currentCell;//游标
private int currentX; //标记当前游标横坐标
private int currentY; //标记当前游标纵坐标

//初始化
public linkListTable(int width, int height)
{
this.width = width;
this.height = height;
//构造height个单链表,长度都是width, 再将他们合并
linkList [] llt;
llt = new linkList[height];
llt[0] = new linkList(width, linkList.ROW);
for(int i=1; i<height; i++)
{
llt[i] = new linkList(width, linkList.ROW);
for(int j=0; j<width; j++)
{
llt[i-1].getElemAt(j, linkList.ROW).goDown 
= llt[i-1].getElemAt(j, linkList.ROW);
}
}
tableTag = llt[0].getElemAt(0, linkList.ROW);
resetCurrentCell();
}

//将table的游标放到最开始的地方
public void resetCurrentCell()
{
currentCell = tableTag;
currentX = 0;
currentY = 0;
}

//游标往右走
public void goRightCell()
{
currentCell = currentCell.goRight;
currentX++;
}

//游标往下走
public void goDownCell()
{
currentCell = currentCell.goDown;
currentY++;
}

//打印二维链表
public void printTable()
{
//分隔线
for(int i=0; i<60; i++)
System.out.print("-");
System.out.println();

resetCurrentCell();
link currentRow = tableTag; //当前行标记
for(int i=0; i<height; i++)
{
for(int j=0; j<width; j++)
{
System.out.print("\t" + currentCell.data);
goRightCell();
}
//跳到下一行的开始
currentRow = currentRow.goDown;
currentCell = currentRow;
currentX = 0;
currentY++;
System.out.println();
}
}

public void setValueAt(int x, int y, int value)
{
if( x>width-1 || y>height-1 )
{
System.out.println("Out of table bound!");  
return;
}
if(currentX <= x && currentY <= y)
{
while(currentY < y)
goDownCell();
while(currentX < x)
goRightCell();
currentCell.data = value;
}
else //如果游标不能"顺水"走到指定位置,将其设置为起始值
{
resetCurrentCell();
setValueAt(x, y, value);
}

}

public static void main(String [] args)
{
linkListTable myTable = new linkListTable(7, 10);
myTable.printTable();
myTable.setValueAt(2, 2, 22);
myTable.printTable();
}
}
输出结果是:
------------------------------------------------------------
        0       0       0       0       0       0       0
        0       0       0       0       0       0       0
        0       0       0       0       0       0       0
        0       0       0       0       0       0       0
        0       0       0       0       0       0       0
        0       0       0       0       0       0       0
        0       0       0       0       0       0       0
        0       0       0       0       0       0       0
        0       0       0       0       0       0       0
        0       0       0       0       0       0       0
------------------------------------------------------------
        0       0       22      0       0       0       0
        0       0       22      0       0       0       0
        0       0       22      0       0       0       0
        0       0       22      0       0       0       0
        0       0       22      0       0       0       0
        0       0       22      0       0       0       0
        0       0       22      0       0       0       0
        0       0       22      0       0       0       0
        0       0       22      0       0       0       0
        0       0       22      0       0       0       0
Press any key to continue...