2017-04-05 33 views
0

初學者程序員在這裏,我目前有一個程序寫成,它接受一個數組和一個變量並比較這兩個數,如果聲明的變量在數組內,則數組將打印出所有的數字除了包含在變量中的數字之外的數組內。這是正常工作和正常運作。此外,我現在想要做的是將我編寫的代碼放在一個可用的方法中重用,但目前我堅持這樣做。最終的結果是顯示一個新的更小的數組,不再包含數字17。 這是我的代碼示例。Array與變量方法調用相比

public class arrayTest 
{ 
    public static void main(String [] args) 
    { 
     int[]arrayA = {2,4,8,19,32,17,17,18,25,17}; 

     int varB = 17; 

     for(int B = 0; B < arrayA.length; B++) 
     { 
     if(arrayA[B] != varB) 
     { 
      System.out.print(arrayA[B] + " "); 
     } 

     } 
    } 

     public static int newSmallerArray(int[] A, int b) 
     { 

     } 
} 
+0

爲什麼你有返回類型的int?不應該像在主要方法中那樣打印它嗎?換句話說,返回類型的void將是合適的或者返回一個數組。 –

+1

你想返回更小的數組,還是隻打印它?另外你的IDE有一個「提取方法」的重構選項,可能 –

+0

請參考這篇文章[鏈接](http://stackoverflow.com/questions/5098540/delete-element-from-array)它與你的問題相同。 –

回答

0

只需重構包含主邏輯並將其移至方法的代碼即可。使用IDE很容易。否則,您必須將代碼複製到新方法並傳遞必要的參數。

public class arrayTest 
{ 
    public static void main(String [] args) 
    { 
     int[] arrayA = {2,4,8,19,32,17,17,18,25,17}; 
     int varB = 17; 
     // Call with the array and variable you need to find. 
     newSmallerArray(arrayA, varB); 
    } 

    public static void newSmallerArray(int[] arrayA, int varB) 
    { 
     for(int B = 0; B < arrayA.length; B++) 
     { 
     if(arrayA[B] != varB) 
     { 
      System.out.print(arrayA[B] + " "); 
     } 
     } 
    } 
} 
+0

由於@ scary-wombat的建議,你需要遵循java命名約定。 – prasanth

2

好吧,在這一點上

int[]arrayA = {2,4,8,19,32,17,17,18,25,17}; 
    int varB = 17; 

你可以叫

newSmallerArray(arrayA, varB); 

,然後,在這個方法中,你將有相同的循環代碼,但變量名稱已更改

for(int B = 0; B < A.length; B++) 
    { 
    if(A[B] != b) 
    { 
     System.out.print(A[B] + " "); 
    } 
    } 

現在麻煩E是聲明瞭這個方法返回一個int但沒有什麼回報,所以也許使它返回void

public static void newSmallerArray(int[] A, int b) 

接下來,我還要堅持到Java命名約定

的完整代碼樣本返回使用更好的名稱

public static void main(String[] args) throws SocketException { 

    int[]arrayA = {2,4,8,19,32,17,17,18,25,17}; 
    int varB = 17; 

    int [] na = newSmallerArray(arrayA, varB); 
    // test 
    System.out.println(); 
    for(int b = 0; na != null && b < na.length; b++) 
    { 
     System.out.print(na[b] + " "); 
    }   
} 

private static int[] newSmallerArray(int[] a, int b) { 

    int count = 0; 
    for(int x = 0; x < a.length; x++) 
    { 
     if(a[x] != b) 
     { 
      System.out.print(a[x] + " "); 
      count++; 
     } 
    } 

    if (count > 0) { 
     int[] newArray = new int [count]; 
     count = 0; 
     for(int x = 0; x < a.length; x++) 
     { 
      if(a[x] != b) 
      { 
       newArray[count++] = a[x]; 
      } 
     }   

     return newArray; 
    } 

    return null; 

} 
輸入數組

的一個子集10

+0

我希望它返回數組中沒有17的數組 – Clark1776

+0

@ Clark1776你需要用你剛寫的評論來更新你的文章。 –

+0

看到我編輯的答案 –

1

我想它沒有17數組中返回數組了

然後,假設你正在使用Java 8+,使用IntStreamfilterb值。 Like,

public static int[] newSmallerArray(int[] arr, int b) { 
    return IntStream.of(arr).filter(x -> x != b).toArray(); 
} 

如果您想要在沒有Java 8功能的情況下執行此操作,則可以計算要排除的值的出現次數。 Like,

private static int countValue(int[] arr, int b) { 
    int count = 0; 
    for (int value : arr) { 
     if (value == b) { 
      count++; 
     } 
    } 
    return count; 
} 

然後使用它來正確調整一個新數組的大小,然後複製這些值。像,

public static int[] newSmallerArray(int[] arr, int b) { 
    int[] ret = new int[arr.length - countValue(arr, b)]; 
    for (int i = 0, p = 0; i < arr.length; i++) { 
     if (arr[i] != b) { 
      ret[p++] = arr[i]; 
     } 
    } 
    return ret; 
}