Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中将ArrayList从一个类传递到另一个类_Java_Arraylist - Fatal编程技术网

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

在Java中将ArrayList从一个类传递到另一个类,java,arraylist,Java,Arraylist,我是Java新手,所以我的问题很基本。我在一个类中有一个数组列表,我需要将它传递给另一个类。我该怎么做 这就是我初始化ArrayList的方式- public class Main { public static void main(String[] args) throws IOException { File file = new File("C:\..."); List<Character> aChars = new ArrayList

我是Java新手,所以我的问题很基本。我在一个类中有一个数组列表,我需要将它传递给另一个类。我该怎么做

这就是我初始化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);
        }
    }
}
公共类主{
公共静态void main(字符串[]args)引发IOException{
File File=新文件(“C:\…”;
List aChars=new ArrayList();
BufferedReader=new BufferedReader(new InputStreamReader(new FileInputStream(file),StandardCharsets.UTF_8));
INTC;
而((c=reader.read())!=-1){
添加((字符)c);
}
}
}

这是我的主类,我应该如何初始化另一个类,以便能够访问arrayList achars

如果希望另一个类访问aChars,可以添加setter。如果需要其他类访问aChars,则应将其添加到构造函数中

需要列表

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);
        }
    }
}
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);
            }
        }
    }
}
Class-MyClass{
私人名单麦克哈斯;
//请注意,如果不提供数组,就无法构造MyClass
公共MyClass(列表字符){
麦克哈斯=查尔斯;
如果(mChars==null){
抛出新的IllegalStateException(“字符不能为null”);
}
}
私人无效文件(){
//现在您可以安全地使用可用的阵列(尽管可能是空的)
for(字符c:mChars){
系统输出打印ln(c);
}
}
}
不需要列表

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);
        }
    }
}
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);
            }
        }
    }
}
Class-MyClass{
私人名单麦克哈斯;
//注意构造函数如何不需要数组
公共MyClass(){
}
公共无效设置字符(列表字符){
麦克哈斯=查尔斯;
}
//注意,我们必须进行空检查
私人无效文件(){
如果(mChars!=null){
//现在您可以安全地使用可用的阵列(尽管可能是空的)
for(字符c:mChars){
系统输出打印ln(c);
}
}
}
}
请注意,它们之间的区别在于使用类是否需要数组。

您会问:

这是我的主类,我应该如何初始化另一个类,以便能够访问arrayList achars

首先也是最重要的一点是,将大部分代码放在主方法之外。相反,您需要创建符合面向对象的类,这些类具有状态(实例字段)和行为(非静态方法)

例如,您可以为类提供一个方法,该方法读取文件并返回感兴趣的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;
}
public List getCharsFromFile(文件){
List aChars=new ArrayList();
//..读取文件格式并将数据放入aChars的代码
返回阿克斯;
}

然后在以后的程序中,您可以调用此方法,在需要时传入文件。

将其传递给另一个类是什么意思?如果您正在调用一个函数,那么执行
functioncall(aChars)
会很好,假设函数编写正确,您真的应该提供更多的上下文,例如要将数组传递给的类的类定义是什么?您通常会将信息作为参数传递给另一个类中的方法。或者,您可以通过调用另一个类中返回必要数据的方法从该类中获取信息。您可以通过构造函数或方法参数来实现这一点。但鉴于你的问题缺乏细节,我们甚至不能开始告诉你任何细节。请大大改进它。请浏览、和部分,了解此网站的工作原理,并帮助您改进当前和未来的问题,这可以帮助您获得更好的答案。我已对代码进行了格式设置,以确保可读性,但今后我强烈建议您自己这样做,尤其是通过给代码适当的缩进,通常每个块有4个空格,并确保同一块上的所有代码都处于相同的缩进级别。格式化是非常重要的,因为如果代码不是标准的可接受格式,它就不是非常可读的,如果它不可读,它就不可理解。