2010-11-23 56 views
1

我正在嘗試編寫一個程序,該程序在執行時將通過一個數組並刪除所有0.0的實例,並將該數組的大小改爲等於非零元素的數量,並將這些元素以先前的順序排列。即,如果n = 10和[J]中的內容,j = 0到N - 1最初是Java從數組中刪除零

0.0, 1.2, 0.0, 0.0, 0.0, 2.3, 0.0, 9.7, 5.6, 0.0 

則代碼執行後的內容應該是

n=4, a[0]=1.2, a[1]=2.3, a[2]=9.7, and a[3]=5.6. 

這是我到目前爲止:

import java.util.Scanner; 
public class hw2 
{ 
    public static void main(String[] args) 
    { 
     Scanner scan = new Scanner(System.in); 
     final double KEY = 0.0; 
     int n = scan.nextInt(); 
     double[] a = new double[n]; 
     for(int i=0; i<n; i++) 
     { 
      a[i] = scan.nextDouble(); 
     } 
     for(int k = 0; k<n; k++) 
     { 
      if(a[k] == KEY) 
      { 
       a[k] = a[k+1]; 
       n--; 
      } 
      System.out.println(a[k]); 
     } 
    } 
} 

只是在正確的方向微調,將不勝感激。

+0

*咳嗽*考慮ArrayList *咳嗽*雖然在這種情況下,如果它是「0」,您可以簡單*不保存* , 也許。 (除非添加,否則不要增加「已使用的數組變量」,因此您知道多少包含「非0」數據或將迭代期間遇到的第一個「0」視爲「有用數據結束」 - 數組是) – 2010-11-23 02:19:16

+0

那麼它的基礎是,如果已經有一個名爲a的數組具有double值,並且您必須運行一段代碼片段,該代碼片段將穿過已填充的數組並刪除所有0.0的實例保持其他數字的順序不變。 – Mike 2010-11-23 02:22:33

回答

1

你的實現(第二for循環)是不正確的,它將無法簡單的測試案例: 輸入> 5 2.0 2 0.0 3 0.0 你的程序將有錯誤的輸出: 2.0 2.0 3.0 3.0

,但它應該是2.0 2.0 3

此外,您不能使用==來比較兩個雙。

下面的代碼是你當前的代碼我的解決方案築底:

public class hw21 { 
    public static void main(String[] args) { 
     Scanner scan = new Scanner(System.in); 
     final double KEY = 0.0; 
     final Double ACCEPTABLE_TOLERANCE = 0.000000000001d; 

     int n = scan.nextInt(); 
     double[] a = new double[n]; 
     for (int i = 0; i < n; i++) { 
      a[i] = scan.nextDouble(); 
     } 
     for (int k = 0, j = 0; k < n; k++) { 
      if (Math.abs(a[k] - KEY) < ACCEPTABLE_TOLERANCE) { 
       continue; 
      } 
      a[j] = a[k]; 
      System.out.println(a[j]); 
      j++; 
     } 
    } 
} 

此外,我更喜歡使用象下面這樣一個ArrayList:

public class hw2 { 
public static void main(String[] args) { 
    Scanner scan = new Scanner(System.in); 
    final double KEY = 0.0; 
    final Double ACCEPTABLE_TOLERANCE = 0.000000000001d; 

    int n = scan.nextInt(); 
    double[] a = new double[n]; 
    for (int i = 0; i < n; i++) { 
     a[i] = scan.nextDouble(); 
    } 

    List<Double> newList = new ArrayList<Double>(); 
    for (int k = 0; k < n; k++) { 
     if (Math.abs(a[k] - KEY) < ACCEPTABLE_TOLERANCE) { 
      continue; 
     } 
     newList.add(a[k]); 
    } 

    System.out.println("There are " + newList.size() + " no-zero double:"); 
     System.out.println(newList); 
    } 
} 
2

考慮使用ArrayList,這將允許您在需要時添加項目,根據需要進行增長並保持準確的計數。

雖然在這種情況下,如果你想/需要使用一個數組,你可能根本不保存值,如果它是「0」,也許。 (除非添加,否則不要增加「已使用的數組變量」,因此您知道多少包含「非0」數據或將迭代期間遇到的第一個「0」視爲「有用數據結束」 - 數組是使用該類型的默認值進行初始化)

如果您想從具有零的數組轉換爲完全不帶零的數組,則必須使用兩個傳遞 - 一個用於計算非零,以構造一個適當大小的新數組,然後複製非零值。這也可以在相反的情況下完成(壓縮初始數組,然後複製它的「已填充」部分),但稍微複雜一些。

如果你與你目前的做法繼續(其結果數組將零,但在年底),你需要保持索引指針 - 一個是主循環迭代,第二個是下一個地方放置非零值,只有在複製值(或不移動,因爲兩個索引相同時,纔會遇到前面的0)時才增加該值。確保將零點移動到非零位置。如果訂單不需要保留,可以減少的移動次數

1
import java.util.Arrays; 
import java.util.Scanner; 

public class StackOverflow1 
{ 
    public static final double KEY = 0.0; 
    private static final Scanner INPUT = new Scanner(System.in); 

    public static void main(String[] args) { 


     int length = INPUT.nextInt(); 
     double[] array = new double[length]; 

     for(int i=0; i<length; i++) { 
      array[i] = INPUT.nextDouble(); 
     } 

     int index = 0; 
     for(int k = 0; k < length ; k++) { 
      if(array[k] == KEY) { 
       continue; 
      } 
      array[index] = array[k]; // bring the non-zeroth element forward 
      if (index != k) array[k] = 0; //make the non-zeroth element zero in the actual location 
      index++; 
     } 
     System.out.println("n = " + index + " array = " + Arrays.toString(array)); 
    } 
} 
0

您可以按如下方式刪除那些不需要的零,但在這種情況下它會被排序。

@org.junit.Test 
public void test15() throws Exception { 
    double[] arr = new double[]{0.0,1.1,0.1,0.0,2.1}; 
    double[] nonZeroArr = arr; 

    Arrays.sort(nonZeroArr); 
    int index = -1; 
    while((index = Arrays.binarySearch(nonZeroArr, 0.0)) > -1){ 
     double[] newArr = new double[nonZeroArr.length-index-1]; 
     System.arraycopy(nonZeroArr, index+1, newArr, 0, newArr.length); 
     nonZeroArr = newArr; 
    } 
    for (double d : arr) { 
     System.out.print(d +","); 
    } 
    System.out.println(); 
    for (double d : nonZeroArr) { 
     System.out.print(d + ","); 
    } 
} 
0

如果你想要的聲音在課堂上點真聰明這是一個在很多語言比Java生產效率更高的單線程:)