简体   繁体   中英

Passing an ArrayList from one class to another in Java

I'm new to Java so my question is very basic. I have an array list in one class and I need to pass it to another class. How can I do this?

This is how I have initialized my ArrayList-

public class Main {

    public static void main(String[] args) throws IOException {
        File file = new File("C:\...");
        List<Character> aChars = new ArrayList<Character>();

        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
        int c;
        while((c = reader.read()) != -1) {
            aChars.add((char) c);
        }
    }
}

This is my main class, how should I initialize another class such that I will be able to access the arrayList achars?

If you want the other class to have access to aChars, you can add a setter. If you require the other class to have access to aChars, you should add it to the constructor.

Requires the List

Class MyClass {
    private List<Character> mChars;

    // note how you cannot construct MyClass without providing the array
    public MyClass( List<Character> chars ) {
        mChars = chars;
        if ( mChars == null ) {
            throw new IllegalStateException("chars cannot be null");
        }
    }

    private void doStuff() {
        // now you can safely use the array is available (though maybe empty)
        for( Character c : mChars ) {
            System.out.println(c);
        }
    }
}

Does not require the List

Class MyClass {
    private List<Character> mChars;

    // note how the constructor does not require the array
    public MyClass() {
    }

    public void setChars( List<Character> chars ) {
        mChars = chars;
    }

    // notice we had to do a null check
    private void doStuff() {
        if ( mChars != null ) {
            // now you can safely use the array is available (though maybe empty)
            for( Character c : mChars ) {
                System.out.println(c);
            }
        }
    }
}

Notice the difference between them is whether or not the array is required for use of the class.

You ask:

This is my main class, how should I initialize another class such that I will be able to access the arrayList achars?

First and foremost, by getting most of that code outside of the main method. Instead you will want to make object-oriented compliant classes, classes that have state (instance fields) and behaviors (non-static methods).

For instance, you could give your class a method that reads from a file and returns the ArrayList of interest:

public List<Character> getCharsFromFile(File file) {
    List<Character> aChars = new ArrayList<Character>();

    //.... code that reads form the file and places data into aChars

    return aChars;
}

and then later in your program you could call this method, passing in the file when needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM