title: Creating a distance map for all stations on a train line
body: <p>I'm revising for a introduction to programming exam and I have a question which from a previous exam paper I am slightly stuck on.</p>
  
  <p>The question:  </p>
  
  <blockquote>
    <p>Write a method that takes a double array as an argument with values representing the positions of train stations along a track. The method should return a two-dimensional array with the distances between each pair of stations in the argument. The array of distances should have only one entry for each pair of stations (i.e. do not use a rectangular array).</p>
  </blockquote>
  
  <p>I have a solution to the question but I just can't get the last bit where there should only be one entry for each pair. I have thought about creating a look up table will all the entries to see if the distance for the two stations but then the array would have lots of empty cells for the later stations because the distance would have already been calculated.   </p>
  
  <p>Here is my current solution</p>
  
  <pre><code>//Set of locations on the train line
  private static double[] stations = {0.0, 2.0, 3.0, 5.0};
  
  //Method to take the array of doubles and create distance map
  public static double[][] getDistances(double[] st){
      double[][] distanceMap = new double[st.length][st.length-1];
      int x;
      for(int i=0; i&lt;st.length; i++){
          x=0;
          for(int j=0; j&lt;st.length; j++){
              if(j != i){
                  distanceMap[i][x] = Math.abs(st[i]-st[j]); 
                  x++;
              }
          }
      }
      return distanceMap;
  }
  
  //Main method to get the distance map then loop over results
  public static void main(String[] args){
      double[][] arrayMatrix = getDistances(stations);
  
      for(int i=0; i&lt;arrayMatrix.length; i++){
          for(int j=0; j&lt;arrayMatrix[0].length; j++){
              System.out.print(arrayMatrix[i][j]+"  ");
          }
          System.out.println("");
      }
  
  }
  </code></pre>
  
  <p>If anyone could point me in the right direction it would be really appreciated.</p>
  
  <p>Thanks in advance.</p>
  
  <p>//EDIT</p>
  
  <p>After some great advice from @izomorphius I have managed to solve the question. Thanks. </p>
  
  <p>Here is the complete solution</p>
  
  <pre><code>//Set of locations on the train line
  private static double[] stations = {0.0, 2.0, 3.0, 5.0};
  
  //Method to take the array of doubles and create distance map
  public static double[][] getDistances(double[] st){
      double[][] distanceMap = new double[st.length-1][];
      int size = st.length-1;
  
      for(int i=0; i&lt;distanceMap.length; i++){
          distanceMap[i] = new double[size];
          size--;
      }
  
      ArrayList&lt;String&gt; lut = new ArrayList&lt;String&gt;();
  
      int x;
      for(int i=0; i&lt;distanceMap.length; i++){
          x=0;
          for(int j=0; j&lt;st.length; j++){
              if(j != i &amp;&amp; !lut.contains(i+"/"+j)){
                  distanceMap[i][x] = Math.abs(st[i]-st[j]); 
                  lut.add(i+"/"+j);
                  lut.add(j+"/"+i);
                  x++;
              }
          }
      }
      return distanceMap;
  }
  
  //Main method to get the distance map then loop over results
  public static void main(String[] args){
      double[][] arrayMatrix = getDistances(stations);
  
      for(int i=0; i&lt;arrayMatrix.length; i++){
          for(int j=0; j&lt;arrayMatrix[i].length; j++){
              System.out.print(arrayMatrix[i][j]+"  ");
          }
          System.out.println("");
      }
  
  }
  </code></pre>