Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java引用_Java_Reference - Fatal编程技术网

java引用

java引用,java,reference,Java,Reference,我的问题是关于幕后参考:) 因此,我试图编写一个递归实现来查找给定ArrayList(getPermsOfMe)的所有置换。我在这里写了两个具体的句子,我在问。我将thisPerm添加到所有置换中,然后在代码中更改thisPerm,然后将所有置换中先前添加的值更改为新值 public ArrayList<ArrayList<Integer>> allPermsHelper(ArrayList<Integer> getPermsOfMe, ArrayList&l

我的问题是关于幕后参考:)

因此,我试图编写一个递归实现来查找给定ArrayList(getPermsOfMe)的所有置换。我在这里写了两个具体的句子,我在问。我将thisPerm添加到所有置换中,然后在代码中更改thisPerm,然后将所有置换中先前添加的值更改为新值

public ArrayList<ArrayList<Integer>> allPermsHelper(ArrayList<Integer> getPermsOfMe, ArrayList<ArrayList<Integer>> allPermutations, ArrayList<Integer> thisPerm){
    if (getPermsOfMe.isEmpty()){
        System.out.println("thisPerm = " + thisPerm);
        allPermutations.add(thisPerm); //LOOK HERE
        System.out.println("allPermutations = " +allPermutations);
    }
    else {
        for (int i = 0; i<ofMe.size(); i++){

            //swapping the two specified elements in getPermsOfMe
            x = getPermsOfMe.get(i);
            getPermsOfMe.set(i, getPermsOfMe.get(getPermsOfMe.size()-1));
            getPermsOfMe.set(getPermsOfMe.size()-1, x);

            if (thisPerm.isEmpty()){
                thisPerm.add(getPermsOfMe.remove(getPermsOfMe.size()-1));
            }
            else{
                thisPerm.add(0,getPermsOfMe.remove(getPermsOfMe.size()-1));
            }

            allPermsHelper(getPermsOfMe, allPermutations, thisPerm);  
            getPermsOfMe.add(0,thisPerm.remove(0)); // LOOK HERE
        }
    } 
    return allPermutations;
}

//an example output (if getPermsOfMe was [123]):
thisPerm = [123]
allPermutations = [[123]]
thisPerm = [231]
allPermutations = [[231],[231]]
thisPerm = [321]
allPermutations = [[321],[321],[321]]
... 
public ArrayList allPermsHelper(ArrayList getPermsOfMe,ArrayList allPermutations,ArrayList thisPerm){
if(getPermsOfMe.isEmpty()){
System.out.println(“thisPerm=“+thisPerm”);
allPermutations.add(thisPerm);//看这里
System.out.println(“allPermutations=“+allPermutations”);
}
否则{

对于(inti=0;i好的,我现在明白了:)这里有一些更好的示例代码:

    ArrayList<Integer> list = new ArrayList<Integer>();
    ArrayList<ArrayList<Integer>> listOfList = new ArrayList<ArrayList<Integer>>();
    list.add(13);
    list.add(42);
    listOfList.add(list);
    System.out.println("list = " + list);
    System.out.println("listOfList = " + listOfList);

    list1.add(46);
    System.out.println("list = " + list);
    System.out.println("listOfList = " + listOfList);
你希望它是:

list = [13, 42]
listOfList = [[13, 42]]
list = [13, 42, 46]
listOfList = [[13, 42]]

问题是,在Java中,列表是对象的集合(或者可能是原语,但在本例中,我们关注的是列表列表,即对象列表).对象包含可修改的数据。如果我们有一个高中学生的列表,并且我们改变了学生的数量,那么这些学生是否也应该在所有将他们直接放入其中的列表中被改变?所以Java就是这样工作的,在幕后使用内存指针。如果你想为一所高中创建一个静态的学生列表,你应该ave必须克隆所有学生,然后将他们放入列表中。类似地,在Java中,您需要克隆对象,以确保其修改不会影响列表。

如何:在幕后,任何适合
对象的内容都只是一个引用,其中包括列表和数组。这与f不同rom原语,如
int
double

int primitive = 14;
int anotherPrimitive = primitive;
System.out.println(anotherPrimitive); // 14
primitive = 12;
System.out.println(anotherPrimitive); // still 14

String[] array = new String[] {"this is a string in the array"};
String[] anotherArray = array;
System.out.println(anotherArray); // ["this is a string in the array"]
array[0] = "now i've changed it";
System.out.println(anotherArray); // ["now i've changed it"]
恰巧,
ArrayList
是一种对象类型,它存储对象引用。这意味着,如果更改它所指向的对象,列表中的对象也会更改

原因:在用户未明确要求的情况下自动复制整个对象图没有多大意义。引用的存储成本非常低(内存中有4或8个字节),复制成本非常低(通常需要一个处理器周期)。每次有人键入
double list时复制1000个
double
的数组。添加(aThousandDoubles);
可能会不必要地浪费大量的时间和内存。此外,可能您不需要不同的引用;可能您有一个
列表监视对象
,如果
列表
实现在每次向列表添加项目时复制对象,您将失去读取更改的能力


因此,让天真的实现简单地将对现有实例的引用存储在
add
上,并让程序员在需要复制对象时复制对象是有意义的。

如果您知道它不正确,并且知道如何修复它,那么为什么不修复它呢?我确实修复了它。我是trying试图理解理论方面。“为什么(在对象和引用方面)之前的附加值不成立?”你所说的“成立”是什么意思?嗯……你写了“注意:我知道这个代码不正确,我知道如何修复它。”在你的问题中。那么删除它。如果它是固定的,不要说它不是固定的。:Pok我会更改它。Alex,我的意思是,为什么以前的值在添加后不会保持不变。所以首先我添加了[123]。接下来我添加了[231],但随后所有的置换都是[[231][231]],而不是[[123][231 231]].谢谢你的帮助!哇,谢谢你把事情弄清楚了!哦,谢谢你的示例代码。下次我会像那样发布我的问题
int primitive = 14;
int anotherPrimitive = primitive;
System.out.println(anotherPrimitive); // 14
primitive = 12;
System.out.println(anotherPrimitive); // still 14

String[] array = new String[] {"this is a string in the array"};
String[] anotherArray = array;
System.out.println(anotherArray); // ["this is a string in the array"]
array[0] = "now i've changed it";
System.out.println(anotherArray); // ["now i've changed it"]