#How to pass an array variable from one class to another?

9 messages · Page 1 of 1 (latest)

distant trench
#

I am working on arrays and wanted to create separate classes for separate methods...i made a class of taking input of all the elements of arrays...and on the second class i want to get that array which was input by the user..Please help me... here's my code :

import java.util.Scanner;

public class makeArrayMethod {
public static void main() {

    Scanner in = new Scanner(System.in);
    int an, i, n;
    System.out.println("Size of the Array : ");
    an = in.nextInt();

    int[] arr = new int[an];

    System.out.println("Enter the number in the array : ");

    for (i = 0; i < arr.length; i++) {
        n = in.nextInt();
        arr[i] = n;
    }
}

}

cosmic juncoBOT
#

This post has been reserved for your question.

Hey @distant trench! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

distant trench
#
import java.util.Scanner;

public class makeArrayMethod {
    public static void main() {

        Scanner in = new Scanner(System.in);
        int an, i, n;
        System.out.println("Size of the Array : ");
        an = in.nextInt();

        int[] arr = new int[an];

        System.out.println("Enter the number in the array : ");

        for (i = 0; i < arr.length; i++) {
            n = in.nextInt();
            arr[i] = n;
        }
    }
}
olive token
#

declare the array as a field and use a getter to access it from the other class.

distant trench
#

Okie Dokie 🫡

olive token
distant trench
#

Thank you

cosmic juncoBOT
# distant trench Thank you

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.