简体   繁体   English

在 Java 中将 ArrayList 从一个类传递到另一个类

[英]Passing an ArrayList from one class to another in Java

I'm new to Java so my question is very basic.我是 Java 新手,所以我的问题非常基本。 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-这就是我初始化我的 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?这是我的主类,我应该如何初始化另一个类以便能够访问 arrayList achars?

If you want the other class to have access to aChars, you can add a setter.如果您希望其他类可以访问 aChars,则可以添加一个 setter。 If you require the other class to have access to aChars, you should add it to the constructor.如果您需要其他类访问 aChars,则应将其添加到构造函数中。

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?这是我的主类,我应该如何初始化另一个类以便能够访问 arrayList achars?

First and foremost, by getting most of that code outside of the main method.首先,通过在 main 方法之外获取大部分代码。 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:例如,您可以为您的类提供一个从文件读取并返回感兴趣的 ArrayList 的方法:

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.然后在你的程序中你可以调用这个方法,在需要时传入文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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