-1

I am trying to figure out to reference an array in another class and having lots of fun. Can anyone help..? This is the code.

As I understand it the getter getName is working for a string, but I do not know how to reference an array with the getter 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";

}

This is the other class and where I want the array value to replace aff_array.class.getName() with aff_array[] getAfz() but I dont know how to do it or reference afz(1) for example (getName is working)

 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();
}
ADTC
  • 8,999
  • 5
  • 68
  • 93
sunirmalya
  • 67
  • 2
  • 9

5 Answers5

1

what about this:

public String getAfz(int i) {
  return aff_array[i];
}
Xavier Coulon
  • 1,580
  • 10
  • 15
0

You simply need to do just :

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

It is required to populate the array before accessing it on the same instance instance.

Sachin Verma
  • 3,712
  • 10
  • 41
  • 74
0

It appears you want to make it static. In that case, you should declare the array as:

private static aff_array[] afz;

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

Then you can access the array as follows:

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

Take note:

aff_array.class.getName() is not calling your custom getName() method. It is calling java.lang.Class.getName().

To call your custom getName() you should call aff_array.getName() (if static) or myArray.getName() where myArray is an instance of aff_array.

Community
  • 1
  • 1
ADTC
  • 8,999
  • 5
  • 68
  • 93
  • it compiles but crashes – sunirmalya Jan 20 '14 at 10:54
  • Please be more specific... it's not clear what you're trying to achieve so we are all doing our best guesses to help you. But your question and comments are too vague... – ADTC Jan 20 '14 at 15:17
0

aff_array mainArray = new aff_array(); mainArray. afz(0).getName(); You can get in this way

Rama
  • 1,156
  • 1
  • 7
  • 13
0

The getAfz() method returns an entire array but since you need a specific entry in an array, you could either initialize the array and then reference it:

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

Or, you could create a method that returns a specific object inside an array:

    public aff_array[] getAfz(int i) {
        return add_array[1];
    }
ucsunil
  • 7,378
  • 1
  • 27
  • 32