ArrayList和Linked列表,所有元素都相同

时间:2018-01-23 17:47:08

标签: java arraylist linked-list

我想制作解决8皇后问题的程序。我做了"表"类,并开始经历8次迭代,并使所有可能的表组合,以便没有任何一个皇后可以互相攻击。但是在迭代结束时以某种方式列出的列表中填充了相同的元素,并且我用作下一个迭代的表(表格表)也作为下一个的起点进行了更改。 我已经看过有关ArrayList问题的帖子,但是所有这些都是通过在循环中创建对象的新实例来解决的,但是这里新的实例是在循环内部进行的。

班级表

package mixony;

public class Table 
{

    private char[][] table;
    private final int WIDTH = 8;
    private final int HEIGHT = 8;
    private final char COVERED = '+';

    public Table() 
    {
        table = new char[HEIGHT][WIDTH];
    }

    public char getField(int x, int y)
    {
        return table[y][x];
    }

    public void setQueen(int x, int y)
    {
        //SET THAT TILE AS QUEEN TILE
        table[y][x]='Q';

        //GO TROUGH THAT COLUMN AND FILL ALL THE COVERED TILES
        for(int i=0; i<HEIGHT; i++)
            if(table[i][x]!='Q')
                table[i][x]=COVERED;

        //GO TROUGH THAT ROW AND FILL ALL THE COVERED TILES
        for(int i=0; i<WIDTH; i++)
            if(table[y][i]!='Q')
                table[y][i]=COVERED;

        //GO TROUGH THAT DOWN VERTICAL AND FILL ALL THE COVERED TILES
        int factor = (x<y)?x:y;
        int x1=x-factor;
        int y1=y-factor;
        for (int i = 0; (x1 + i) < WIDTH && (y1 + i) < HEIGHT; i++)
            if(table[y1+i][x1+i]!='Q')
                table[y1+i][x1+i]=COVERED;

        //GO TROUGH THAT UP VERTICAL AND FILL ALL THE COVERED TILES
        x1=x;
        y1=y;
        while(true)
        {
            if(x1==0 || y1==WIDTH-1)
                break;
            x1--;
            y1++;
        }
        for (int i = 0; (x1 + i) < WIDTH && (y1 - i) > 0; i++)
            if(table[y1-i][x1+i]!='Q')
                table[y1-i][x1+i]=COVERED;


    }

    public char[][] getTable() {
        return table;
    }

    public void setTable(char[][] table) {
        this.table = table;
    }

    public void log(int index)
    {
        System.out.println("\nTABLE "+index+"\n\t---------------------------------");
        for (int y=0; y<HEIGHT; y++)
        {
            System.out.print("ROW "+y+"\t|");
            for (int x=0; x<WIDTH; x++)
            {   

                System.out.print(" "+getField(x, y)+" |");
            }
            System.out.print("\n\t---------------------------------\n");
        }
    }

}

使用main方法的类

package mixony;

import java.util.LinkedList;

public class PlayTest 
{
    public static void main(String[] args)
    {
        LinkedList<Table> tables = new LinkedList<Table>();
        LinkedList<Table> improvedTables = new LinkedList<Table>();
        tables.add(new Table());
        final Table table = tables.get(0);
        for(int y=0; y<8; y++)
        {
            for(int x=0; x<8; x++)
            {
                if(table.getField(x, y)!='Q'
                && table.getField(x, y)!='+')
                {
                    Table imp = new Table();
                    imp.setTable(table.getTable());
                    imp.setQueen(x, y);
                    improvedTables.add(imp);
                    table.log(0);

                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我在你的代码中看到的一个问题是你不复制LinkedList但是引用它:

LinkedList<String> firstList = new LinkedList<>();
LinkedList<String> secondList;

secondList = firstList;//Dont do that because Call by Reference!

firstList.add("Test");//Everything i do to firstList affects secondList
System.out.println(secondList.size());//prints 1

相反,您应该通过复制构造函数复制LinkedList

LinkedList<String> firstList = new LinkedList<>();
LinkedList<String> secondList;

secondList = new LinkedList<>(firstList);//Copy the List instead of referencing it

firstList.add("Test");//Everything i do to firstList affects only firstList
System.out.println(secondList.size());//prints 0