简体   繁体   English

从Java中的另一个类存储数组

[英]Storing array from another class in java

I know that there's tons of ways to store an array from another class but what is the best approach that can be easily understood by everyone, even beginners? 我知道有很多方法可以存储另一个类中的数组,但是什么是所有人甚至初学者都可以轻易理解的最佳方法?

Here is the sample code: 这是示例代码:

1st class: // this is where to get the array to be stored 第一类://这是获取要存储的数组的位置

public class Example{
    private int[] Numbers = {3,2,1}//sample numbers        
}

2nd class: // this is where the array to be stored 2nd class://这是要存储数组的位置

public class Example2{
    public void Storage{
        int[] NumberStorage = /* this is where the Numbers from the
                                          1st class to be stored */
    }
}

In your Example class create a getter : 在您的Example类中,创建一个getter

public int[] getNumbers() { return Numbers; }

Then in your Example2 class you can instantiate an instance of Example and call the getter like: 然后,在您的Example2类中,可以实例化Example的实例,然后像这样调用getter

public void storage(){
    Example example = new Example();
    int[] NumberStorage = example.getNumbers();
}

This might be what You want 这可能就是您想要的

First Class public class Class1{ 头等舱公共舱Class1 {

private int [] Array = { 1,2,3,2,3,4};

public  Class1(){


   Class2 a =  new Class2();

   a.Class2(Array,6);
 }}

second class public class Class2 { public void A( Array, size) //enter Sort here } } 第二类public class Class2 {public void A(Array,size)//输入此处的排序}}

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

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