静态方法将值返回给所有变量

时间:2019-03-05 11:41:05

标签: java arraylist

我目前在我的Java代码上遇到以下问题。我一直在尝试调用方法trHead(),该方法获取参数arraylist作为参数,并删除列表的最后3个项目然后返回它。是当我运行我的代码并为arraylist A调用trHead()方法时,似乎工作得很好,但实际上,它不仅改变了arraylist A,而且改变了arraylist B,甚至没有为arraylist B调用它。

我的代码如下:

import java.util.ArrayList;

public class S1Calculation {

private static final int δ = 30;
private static final double ε = 0.9;
private int LCSScount = 0;  

    //Calculate the LCSS for any two trajectories
    public int LCSS(ArrayList<Double> A, ArrayList<Double> B , ArrayList<Integer> tA , ArrayList<Integer> tB) {


        if((A.size()==0) || (B.size()==0)) {
            return LCSScount;
        }

        if((Math.abs(A.get(A.size()-1)-B.get(B.size()-1))<ε) && (Math.abs(A.get(A.size()-2)-B.get(B.size()-2))<ε) && (Math.abs(A.get(A.size()-3)-B.get(B.size()-3))<ε) && (Math.abs(tA.get(tA.size()-1)-tB.get(tB.size()-1))<=δ)){
            ArrayList<Double> tmpA = new ArrayList<Double>();
            ArrayList<Double> tmpB = new ArrayList<Double>();
            ArrayList<Integer> tmptA = new ArrayList<Integer>();
            ArrayList<Integer> tmptB = new ArrayList<Integer>();
            System.out.println(A.size());
            System.out.println(B.size());
            tmpA = trHead(A);
            System.out.println(A.size());
            System.out.println(B.size());
            tmpB = trHead(B);
            System.out.println(A.size());
            System.out.println(B.size());
            tmptA = tmHead(tA);
            tmptB = tmHead(tB);
            System.out.println("yes");
            return (1+LCSS(tmpA,tmpB,tmptA,tmptB));
        }
    }

    public static ArrayList<Double> trHead(ArrayList<Double> Array) {
        System.out.println("im in");
        Array.remove(Array.size() -1);
        Array.remove(Array.size() -1);
        Array.remove(Array.size() -1);
        return Array;
    }

    public static ArrayList<Integer> tmHead(ArrayList<Integer> Times){
        Times.remove(Times.size()-1);
        return Times;
    }
}

我的主要任务是:

public class Main {

public static void main(String[] args){
    //Parse some csv files and insert them in a 2 dimensional arraylist
    File dir = new File("\\Path\\");
    List<ArrayList<Double>> Trajectories = new ArrayList<ArrayList<Double>>();
    List<ArrayList<Integer>> Timestamps = new ArrayList<ArrayList<Integer>>();
    for (File file : dir.listFiles()) {
        Trajectory tr1 = new Trajectory(file);
        Trajectories.add(tr1.getTrajectory());
        Timestamps.add(tr1.getTimestamp());
    }
    //Call LCSS method for 4 arraylists (csv files)
    int FinalLCSS = new S1Calculation().LCSS(Trajectories.get(0),Trajectories.get(0),Timestamps.get(0),Timestamps.get(0));

请注意,我正在为相同的csv文件调用LCSS,这意味着传递给LCSS方法的数组列表A和B包含相同的值.tA和tB相同。

当我运行它时,我得到以下结果: enter image description here enter image description here

如您所见,数组列表A调用了trHead方法,但数组列表B的大小也受到了影响,任何帮助都将不胜感激!

1 个答案:

答案 0 :(得分:0)

Java通过引用传递对象。

在调用LCSS(Trajectories.get(0),Trajectories.get(0),...)中,参数ArrayList<Double> AArrayList<Double> B最终引用(指向)同一对象,该对象位于Trajectories位置0

如果要使用单独的对象,则需要进行复制。

ArrayList<Double> tmpA = new ArrayList<>(A);
ArrayList<Double> tmpB = new ArrayList<>(B);

现在tmpAtmpB将包含AB的副本,并且即使AB相同也将有所不同