2014-06-21 42 views
-2

我嘗試在第一個數組中保存一些值,然後在第二個數組中的另一個值,我嘗試打印第一個數組並返回第二個數組值。任何想法爲什麼?爲什麼最後一個數組不填寫第一個數組?

這是主要的方法來調用程序

public class testC 
{ 
    public static void main(String[] args) 
    { 
     groupC trial = new groupC(); 
     trial.setCol(10, 10); 
    } 
} 

這是類調用另一個類來獲得顏色和陣列填充每個新座標

public class groupC 
{ 
    private color1 col = new color1(); 
    private int[] cola1 = new int[3]; 
    private int[] cola2 = new int[3]; 

    public groupC() 
    { 

    } 

    public void setCol(int xIn, int yIn) 
    { 
     cola1 = col.getCol(xIn, yIn); 
     System.out.println(cola1[0] + " " + cola1[1] + " " + cola1[2]); 
     /* try next color depending on 1st */ 
     cola2 = col.getCol(xIn + 100, yIn + 100); 
     System.out.println(cola2[0] + " " + cola2[1] + " " + cola2[2]); 

     System.out.println("this is 1st color, but why now the same as 2nd ?" + cola1[0] + " " + cola1[1] + " " + cola1[2]); 
    } 
} 

這是一流的,它只是變得協調和返回當前顏色值的陣列

import java.awt.Color; 
import java.awt.Robot; 
import java.awt.AWTException; 

public class color1 
{ 
    int[] color = new int[3]; 

    public color1() 
    { 
    } 

    public int[] getCol(int xIn, int yIn) 
    { 
     // accepts position of color, returns size 3 array of red green blue 
     // integers 
     try 
     { 
      Robot r = new Robot(); 
      Color x = r.getPixelColor(xIn, yIn); 
      color[0] = x.getRed(); 
      color[1] = x.getGreen(); 
      color[2] = x.getBlue(); 
     } 
     catch (AWTException e) 
     { 
      e.printStackTrace(); 
     } 
     return color; 
    } 
} 
+0

我假設你已經證明了'color1'類實際上是你的代碼使用'colorD'類。 –

+1

另外,作爲一個附註 - 用BlockCaseLikeThis命名你的'Type',而不是camelCaseWordJoint。語義,並使您的代碼更可重用:) – Unihedron

+0

是的,當我縮短代碼時,有錯誤,colorD是color1,現在正在修復。 – Ubaby

回答

3

因爲color1重用單個陣列:

public class color1 
{ 
    int[] color = new int[3]; // <== Creates one array for this color1 instance 
    // ... 
} 

所有這一切發生在getCol的是,它被填充(再次):

Color x = r.getPixelColor(xIn, yIn); 
color[0] = x.getRed(); // <==== Nothing here is creating a new color array 
color[1] = x.getGreen(); 
color[2] = x.getBlue(); 

如果你想使用一個以上的數組,你必須實際上創建了多個數組。這可能意味着完全刪除color實例成員,並在getCol中創建陣列:

import java.awt.Color; import java.awt.Robot; import java.awt.AWTException;

public class color1 
{ 
    public color1() 
    { 
    } 

    public int[] getCol(int xIn, int yIn) 
    { 
     // accepts position of color, returns size 3 array of red green blue 
     // integers 
     int[] color = new int[3]; // Has to be here because of how you're (not) handling exceptions 
     try 
     { 
      Robot r = new Robot(); 
      Color x = r.getPixelColor(xIn, yIn); 
      color[0] = x.getRed(); 
      color[1] = x.getGreen(); 
      color[2] = x.getBlue(); 
     } 
     catch (AWTException e) 
     { 
      e.printStackTrace(); 
     } 
     return color; 
    } 
} 
1

你實際上只填充一個陣列 - 每次你打電話getCol時間覆蓋它。因此,cola1cola2的值最後指向相同的數組,而您希望它們引用不同的數組。

或許你也應該建立在getCol一個新的數組,聲明color作爲一個局部變量,而不是一個領域:

// Note rename of method to be more descriptive 
public int[] getColorComponents(int xIn, int yIn) 
{ 
    int[] components = new int[3]; 
    // accepts position of color, returns size 3 array of red green blue 
    // integers 
    try 
    { 
     Robot r = new Robot(); 
     Color x = r.getPixelColor(xIn, yIn); 
     components[0] = x.getRed(); 
     components[1] = x.getGreen(); 
     components[2] = x.getBlue(); 
    } 
    catch (AWTException e) 
    { 
     e.printStackTrace(); 
    } 
    return components; 
} 

(順便說一句,僅通過捕獲來「處理」異常,打印棧跟蹤,然後繼續,就像沒有任何錯誤是幾乎從來沒有正確的方法。此外,你應該工作在你的命名 - 沒有你的類名稱描述他們的目的,都違反了Java命名約定。)

+0

非常感謝你的建設性反饋:) – Ubaby

0

數組變量是Java中的引用,所以如果你將一個數組變量賦值給anot她的一個,並修改其中的一個,那麼其他也被修改:

int[] a = new int[3]; 
int[] b; 
b = a; 
b[0] = 42; 
System.out.println(a[0]); //: 42 

爲了解決這個問題,添加以下的getCol開頭:

color = new int[3]; 

更妙的是:讓color本地的getCol變量:

int[] color = new int[3]; 
相關問題