2010-03-05 165 views
1

我試圖創建一個方法,它將2個int數組作爲輸入參數,如果數組相反則返回true,否則返回false。這是我迄今爲止的,但它是錯誤的。檢查一個數組是否與java中另一個數組的反轉

public static void main(String[] args) 
{ 
    int b,a; 
    int[] data1 = {14,-70,-18,88,85,97,-65,13,-71,-12}; 
    int[] data2 = {-12,-71,13,-65,97,85,88,-18,-70,14}; 
    boolean check = true; 

    for (a=0;a<data1.length;a++) 
    { 
     for (b=data2.length-1;b>=0;b=b-1) 
     { 
        if (data1[a] != data2[b]) 
         check=false 
     } 
    } 
    System.out.println(check); 
} 

我的例子假設打印真實,但它doesn't。我假設2陣列是相同的長度。誰能幫忙?

+0

家庭作業?請相應標記。 – 2010-03-05 07:26:36

回答

7

你不需要兩個循環 - 你只需要一次循環,在一個陣列中使用索引「正常」,並從另一端爲其他數組:

public static boolean checkReversed(int[] x, int[] y) 
{ 
    // For production code, possibly add nullity checks here (see comments) 
    if (x.length != y.length) 
    { 
     return false; 
    } 
    // Loop through x forwards and y backwards 
    for (int i = 0; i < x.length; i++) 
    { 
     if (x[i] != y[y.length - 1 - i]) 
     { 
      // As soon as we've found a "mistake" we can exit: 
      // This is simpler (IMO) than keeping a "check" variable 
      return false; 
     } 
    } 
    return true; 
} 
+0

我明白了。感謝您的建議! – Dan 2010-03-05 06:09:49

+0

爲什麼要添加無效檢查?如果我爲其中一個數組傳遞空值,我想要引發異常。 – Gabe 2010-03-05 06:27:37

+0

@gabe:對於Java,爲True。在C#中,我想拋出ArgumentNullException而不是等待NullReferenceException。我一直忘記Java沒有那個,惱人的:(如果它們是* both * null,你可能想要返回true,儘管如此,可能的話。 – 2010-03-05 06:54:46

1

你可以在一個循環中完成,你不需要兩個循環。

for (int i=0,j=end;i<end;i++,j--)

2
在此

,兩個陣列長度應當相等。然後

for(int i=0,j=array.length;i<array.length,j=0;i++,j--){ 
    write your comparison logic here 
} 
+1

你只需要一個變量 - 而且你的條件有點奇怪,因爲它包含一個任務... – 2010-03-05 06:01:44

+0

oo,我是新來的java,所以我還沒有學習。謝謝您的幫助! :) – Dan 2010-03-05 06:03:17

3

你可以試試:

// compare the length. 
check = (data1.length != data2.length)?false:true; 

// if lengths are equal..go ahead and compare elements in reverse. 
if(check) {  
    for(int i=0,j=data2.length;(i<data1.length) && (j>=0);i++,j--) { 
     // if you find a mismatch..set check to false..and break 
     // no need to compare other ele. 
     if(data1[i] != data2[j]) { 
      check = false; 
      break; 
     } 
    } 
} 
2

你的代碼實際上比較data1中的每個元素與data2中的每個元素,如果有任何一個不匹配,則輸出false。這不是你打算做的。

2

這裏是一個回答你的問題在一個完整的java文件

//yeah.java 
public class yeah { 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 

     int[] data1 = {14,-70,-18,88,85,97,-65,13,-71,-12}; 
     int[] data2 = {-12,-71,13,-65,97,85,88,-18,-70,12}; 

     System.out.println(isReverse(data1, data2)); 
    } 

    public static boolean isReverse(int[] a, int[] b) 
    { 
     if (a.length != b.length) //If a and b are not of the same length how can they be reverse? 
      return false; 
     for (int i=0;i<a.length;i++) 
      if (a[i] != b[a.length-i-1]) 
       return false; 
     return true; 
    } 

} 

只是在方法和功能的快速說明..只要你發現他們得不到扭轉,你應該退出使用return statement ..不需要繼續計算..

+0

+1:好方法。 – 2010-03-05 06:19:55

0

這可以在一個循環中完成,不需要兩個循環。

例如:

for (int i = 0, j = last; i < last; i++, j--) 
相關問題