Java: 无法得到两个不为 "connected" 的 ArrayList

Java: Cannot get two ArrayLists not to be "connected"

所以当我遇到一个问题时,我正在练习 UIL:我的 ArrayLists,出于某种奇怪的原因,彼此共享相同的值。它们都被声明为字符,尽管被声明为不同的对象,但我想不出一种方法来将它们分开。

我想要做的是其中一个将作为基础,而另一个在整个嵌套循环中发生变化,但是,即使我在设置基础后没有更改基础,它们也会改变,并且我不知道为什么。我之前找到了我的答案,解释说主要方法是静态的,所以它们指向同一个地方,但是在创建一个新的 class 然后在主要方法中创建一个对象来完成它仍然无法工作.

任何人都可以向我解释为什么他们有联系吗?

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

public class Boggle 
{
    public static void main (String args[]) throws Throwable
    {
        Boggle test = new Boggle();
        test.theBoggle();
    }

    public void theBoggle() throws Throwable
    {
        Scanner scan = new Scanner(new File("boggle.dat"));
        int games = scan.nextInt();
        int amtofwords = 0;
        int puzzle = 1;
        boolean possible = true;
        boolean possibleletter = false;
        String tempword = "";
        int pointcounter = 0;
        ArrayList<Character> scrambled = new ArrayList<Character>(); 
        ArrayList<Character> testscramble = new ArrayList<Character>();
        ArrayList<String> words = new ArrayList<String>();
        scan.nextLine();
        for (int i = 0; i < games; i++)
        {
            for (int w = 0; w < 4; w++)
            {
                tempword = scan.nextLine();
                scrambled.add(tempword.charAt(0));
                scrambled.add(tempword.charAt(1));
                scrambled.add(tempword.charAt(2));
                scrambled.add(tempword.charAt(3));
            }
            amtofwords = scan.nextInt();
            scan.nextLine();
            for (int k = 0; k < amtofwords; k++)
            {
                words.add(scan.nextLine());
            }
            for (String string : words) //grabs the words
            {
                possible = true;
                testscramble = scrambled; //````````````IMPORTANT WHY ARE THEY BOTH CONNECTED?``````````
                System.out.println(scrambled);
                for (int k = 0; k < string.length(); k++) //grabs the scrambled letters to compare to string
                {
                    for (int j = 0; j < testscramble.size(); j++) //grabs individual letters to compare to word
                    {
                        if (string.charAt(k) == testscramble.get(j))
                        {
                            possibleletter = true;
                            testscramble.remove(j);

                            break;
                        }
                    }
                    if (possibleletter == false)
                    {
                        possible = false;
                        break;
                    }
                    possibleletter = false;

                }
                if (possible == true)
                {
                    if (string.length() <= 2)
                        pointcounter += 0;
                    if (string.length() == 3 || string.length() == 4)
                        pointcounter += 1;
                    if (string.length() == 5)
                        pointcounter += 2;
                    if (string.length() == 6)
                        pointcounter += 3;
                    if (string.length() == 7)
                        pointcounter += 5;
                    if (string.length() >= 8)
                        pointcounter += 11;
                }
            }
            System.out.println("PUZZLE #" + puzzle + " " + pointcounter);
            puzzle++;

        }
        scan.close();
    }
}

如评论中所述,您使用引用将一个列表变量转换为对另一个列表变量的引用

testscramble = scramble;

当你应该复制列表时

testscramble = new ArrayList<>(scrambled);