2014-01-20 42 views
-1

我想弄明白在另一個類中引用數組,並有很多樂趣。任何人都可以幫忙..?這是代碼。ref從另一個類的數組

據我瞭解,吸氣getName正在爲一個字符串,但我不知道如何引用數組與吸氣aff_array[] get afz()

public class aff_array { String name; aff_array[] afz; 

public aff_array[] getAfz() { 
return afz; 
} 

public String getName() { 
return name; 
} 

public static void main (String[] args) { 

    int z = 3; //total no of affirmations 
    int x = 1; 
    aff_array[] afz = new aff_array[z]; //dim 

    while (x < z) { 
     afz[x] = new aff_array(); // create objects for array 
     x = x + 1; 
    } 

    afz[1].name = "i am the best"; 
    afz[2].name = "you are the rest"; 

} 

這是其他類,並在那裏我想要的數組值更換aff_array.class.getName()aff_array[] getAfz()但我不知道該怎麼做或引用afz(1)例如(getName工作)

public void onReceive(Context context, Intent intent) 
{ 

    setNotification(context, aff_array.class.getName()); 
    WakeLocker.acquire(context); 
    Toast.makeText(context,"One shot alarm received. No more toasts will be shown.", Toast.LENGTH_SHORT).show(); 
    WakeLocker.release(); 
} 

回答

1

什麼這一點:

public String getAfz(int i) { 
    return aff_array[i]; 
} 
0

您只需只是做:

aff_array instance = new aff_array(); 
// set the array inside it 
setNotification(context, instance.getAfz()[0].getName()); 

它需要訪問它在同一實例instance之前填充陣列。

+0

感謝 - 它編譯但崩潰 – sunirmalya

+0

崩潰?運行時錯誤?如果它的ArrayIndexOutOfBounds,那麼你需要自己理解或者告訴錯誤信息 –

+0

它似乎是空的 - 我很抱歉 - 我會繼續尋找謝謝 – sunirmalya

0

看來你想使它成爲static。在這種情況下,你應該聲明數組爲:

setNotification(context, aff_array.getAfz()[1].getName()); 

請注意::

private static aff_array[] afz; 

public static aff_array[] getAfz() { 
    return afz; 
} 

然後如下您可以訪問陣列

aff_array.class.getName()打電話給你定製getName()方法。它叫java.lang.Class.getName()

要調用定製的getName(),你應該叫aff_array.getName()(如果靜態)myArray.getName()其中myArrayaff_array一個實例。

+0

它編譯但崩潰 – sunirmalya

+0

請更具體的......不清楚你是什麼我們正在努力實現,所以我們都在盡我們最大的努力來幫助你。但你的問題和意見太模糊了...... – ADTC

0

aff_array mainArray = new aff_array(); mainArray。 AFZ(0).getName(); 您可以通過這種方式獲得

0

的getAfz()方法返回的整個數組,但因爲你需要在數組中的特定條目,你既可以初始化數組,然後引用它:

aff_array aa = new aff_array(); 
    String name = aa.afz[1].getName(); // reference this in your setNotification 

或者,你可以創建一個返回一個特定的對象數組內的方法:

public aff_array[] getAfz(int i) { 
     return add_array[1]; 
    }