I would like to calculate the distance in 4d I have 2 arraylist that have all of my elements in it. How do make the formula i have this but its not calculating correctly the distance. List has diferent coordinates every time and t always has the same coordinates.

        double w = 0.0;

        for (int i = 0; i < List.size(); i++)
        {
            for (int j = 0; j < List.get(i).size(); j++)
          {
           w =   Math.sqrt(Math.pow(List.get(i).get(j) - t.get(j), 2)+Math.pow(List.get(i).get(j) - t.get(j), 2)+Math.pow(List.get(i).get(j) - t.get(j), 2)+Math.pow(List.get(i).get(j) - t.get(j), 2));
          }
        System.out.println(w);

take a moment to think through the value(s) of j on lines 6 and 7.
on each pass of the loop you do the whole calculation for just one value of j, then you do that multiple times.

pseudo-code the solution on paper and step throught it a couple of times... don't write any java code until you are sure you have the logic right.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.