简体   繁体   English

为什么此数组不打印所有值(打印0)?

[英]Why is this array not printing all it's values (Prints 0's)?

Basically the program generates positions of x,y,z coordinates, including a distance index. 基本上,程序会生成x,y,z坐标的位置,包括距离索引。 The program stores them in each specified index, and proceeds to sort them. 程序将它们存储在每个指定的索引中,并进行排序。 The resultant output is sorted but some columns print a bunch of zeroes (As if the array is never generated at that position). 结果输出已排序,但某些列显示一堆零(好像该位置从未生成过数组)。 Any help would be appreciated! 任何帮助,将不胜感激!

Example output : 示例输出:

(0.0),(0.0),(0.0)),(0.0)
(0.0),(0.0),(0.0)),(0.0)
(0.0),(0.0),(0.0)),(0.0)
(0.0),(0.0),(0.0)),(0.0)
(0.0),(0.0),(0.0)),(0.0)
(2.1933026),(3.873999),(2.6528487)),(5.1822824)
(2.1933026),(3.873999),(4.9137025)),(6.6304536)
(3.4213266),(5.4315333),(5.976922)),(8.771011)
(4.084449),(5.4315333),(5.976922)),(9.050297)
(6.4669027),(5.4315333),(5.976922)),(10.346303)

Main program 主程序

//import java.util.ArrayList;

import java.util.*;
public class MyQuickSort {
  private static float a[][] = new float [10][4];

  public static void main(String[] args) {
    System.out.println("(" + "X Length" + "),(" + "Y Length" + "),(" + "Z Length" + "),(" + "Distance Length" + ")");
    for(int i = 0; i < a.length ; i++) {
      for(int j = 0; j < 3; j++) {
        a[i][j] = (float)(Math.random () * 6) +1 ;
        //a[i][3] = (float)Math.sqrt((a[i][0] * a[i][0]) + (a[i][1] * a[i][1]) + (a[i][2] * a[i][2]));
        for (int k = 0; k < 4; k++ ) {
          quickSort(a, 0, a.length - 1,k);
        }
      }
              a[i][3] = (float)Math.sqrt((a[i][0] * a[i][0]) + (a[i][1] * a[i][1]) + (a[i][2] * a[i][2]));
      System.out.println("(" + a[i][0] + "),(" + a[i][1] + "),(" + a[i][2] + ")" + "),(" + a[i][3] + ")"); 
    }
  }

  public static void quickSort(float[][] a, int p, int r,int k)
  {
    if(p<r)
    {
      int q=partition(a,p,r,k);
      quickSort(a,p,q,k);
      quickSort(a,q+1,r,k);
    }
  }

  private static int partition(float[][] a, int p, int r,int k) {

    float d = a[p][k];
    int i = p-1 ;
    int j = r+1 ;

    while (true) {
      i++;
      while ( i< r && a[i][k] < d)
        i++;
      j--;
      while (j>p && a[j][k] > d)
        j--;

      if (i < j)
        swap(a, i, j,k);
      else
        return j;
    }
  }

  private static void swap(float[][] a, int i, int j, int k) {

    float temp = a[i][k];
    a[i][k] = a[j][k];
    a[j][k] = temp;
  }
}

You've mixed array initialization and sorting - that's the problem. 您混合了数组的初始化和排序-这就是问题所在。 On the first iteration, you fill the first element and then run sort, which moves this element to the end of the array, as it's greater than uninitialized zeros. 在第一个迭代中,您填充第一个元素,然后运行sort,将其移动到数组的末尾,因为它大于未初始化的零。 After sorting you print the first element which is now filled with zeros. 排序后,您将打印第一个元素,该元素现在填充了零。 This is still happening till the 6th iteration, when number of initialized elements becomes greater than number or uninitialized elements. 这种情况一直持续到第六次迭代,此时初始化元素的数量大于未初始化元素的数量。

You probably want to split the code in three parts: initialization, sorting, and printing. 您可能希望将代码分为三个部分:初始化,排序和打印。

You sort in the loop. 您在循环中排序。 This means the filled values are sorted to the end, and then later overwritten. 这意味着填充的值将被排序到末尾,然后被覆盖。

Fix: First do the i/j loop to fill, then sort once, then do a separate loop to print the data 修复:首先执行i / j循环以填充,然​​后排序一次,然后执行单独的循环以打印数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打印数组的组件,但它打印 null + 指定索引处的值(JAVA) - Printing an Array's components but it prints null + the value at the specified index (JAVA) 不带全零的因子打印数组 - printing array with factors without all the 0s 为什么选择System.out.println(“嘿s1 == s2:”+ s1 == s2); 打印“false”作为输出而不是打印“hey s1 == s2:false” - Why System.out.println(“hey s1==s2:”+s1==s2); prints “false” as the output instead of printing “hey s1==s2:false” 打印String []会产生随机的null值,不确定为什么会发生 - Printing String[] produces random null values, unsure why it's happening 为什么此功能不能打印所有值? - Why is this function not printing all the values? Java排序错误,打印出一个0数组而不是该数组的值 - Java sorting error, printing out an array of 0s rather than the values of the array JRBeanCollectionDatasource仅打印第一个bean的属性值 - JRBeanCollectionDatasource prints only first bean's propery values 为什么这段代码会打印出无数的“您的猜测是什么?” - Why this code prints an infinite amount of “What's your guess?” 二叉树打印出所有的零 - Binary Tree printing out all zero's 为什么我的 ArrayList 会像调用时一样频繁地打印元素 - Why is my ArrayList prints the elements as frequent as when it's called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM