Ron Tan
Ron Tan

Reputation: 63

Passing an array from class A to class B

I have an array named Floors in class A, it contains values something like the following:

  List<Point> manualpoint = new ArrayList<Point>();
  int[] manualpointx = {135,200,300,155,235,300};

Let's say I wish to pass these values to class B, supposingly class B has already declared a superclass View

public class DrawView extends View implements OnTouchListener { 
    @Override
    public void onDraw(Canvas canvas) {
        //pass the values into this class
    }

}

How do i pass the values from class A to B?

Upvotes: 2

Views: 26326

Answers (4)

Leo Prince
Leo Prince

Reputation: 950

Another one idea is , try having a public class in B and if possible call the B's set Array method with the Array values of A. now in set Array we can have a class variable to save the Array values and then call draw.

public class DrawView extends View implements OnTouchListener {
    int[] manualpointx;
    setArray(int[] manualpointx1){
        this.manualpointx = manualpointx1
    }
     public void onDraw(Canvas canvas) { 
        //pass the values into this class 
        we can use manualpointx 
    }
}

Upvotes: 0

Lion
Lion

Reputation: 19027

You can do something like this.

final class A
{
     private static int[] manualpointx = {135,200,300,155,235,300};
     private static List<Point> manualpoint = new ArrayList<Point>();

     public static int[] returnArray()
     {
          return(manualpointx);   
     }

     public static List<Point> returnList()         
     {
          return(manualpoint);
     }

}
public class DrawView extends View implements OnTouchListener
{ 
    @Override
    public void onDraw(Canvas canvas) 
    {
         //pass the values into this class
         int arr[]=A.returnArray();
         List<Point> list=A.returnList();
    }
}

If you need only non-static fields in your class A and if you just want to use non-static methods, you will have to access them via an instance of the class A from your class DrawView.

Upvotes: 1

Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53647

To pass data between activities

//to pass array use
intent.putExtra("intarray", manualpointx);
// to pass list of point use
intent.putParcelableArrayListExtra("bundle", (ArrayList<? extends Parcelable>) manualpoint);

For classes

create public getter methods to return the values and call those methods from any class where you want to get the values

Upvotes: 2

Deco
Deco

Reputation: 3321

You need to create an instance of Class A and then access the values from Class B, for example:

public class ClassA {
  private int someInt;

  public ClassA() {
    someInt = 5;
  }

  public int getSomeInt() {
    return someInt;
  }
}

public class ClassB {
  public void someMethod() {
    ClassA instanceOfClassA = new ClassA();

    int someIntFromClassA = instanceOfClassA.getSomeInt();  //This is now 5;

    // Rest of your code here
  }
}

Alternatively you can create it as a static method in ClassA and then call it in ClassB by using ClassA.getSomeInt();

Upvotes: 0

Related Questions