java 是否可以创建全局数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16767186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:57:24  来源:igfitidea点击:

Is possible to create global array?

javaandroid

提问by anze87

I need to fill data in 50 instances of DataPack.class let's say in class A, but I need to read out that data in some class B. Class DataPack looks like this:

我需要在 DataPack.class 的 50 个实例中填充数据,假设在 A 类中,但我需要在 B 类中读取该数据。类 DataPack 如下所示:

public class DataPack {
    int fNumber;
    int dateTime;
    int Year, fMonth, fDay;
    int fTimeHours, fTimeMin, fTimeSec;
    int fSize;
    char[] name = new char[18];
    char[] surname = new char[18];
}

In class A I would create DataPack[] mDataPack = new DataPack[50]; and then fill data in each array member. But for reading in class B, this data array will need to be global. Is this possible to solve this in that way? Or exists better solutions?

在类 AI 中将创建 DataPack[] mDataPack = new DataPack[50]; 然后在每个数组成员中填充数据。但是为了在 B 类中读取,这个数据数组需要是全局的。这有可能以这种方式解决吗?或者存在更好的解决方案?

Thanks for help!

感谢帮助!

回答by Chintan Khetiya

Make a ClassicSingleton.javaclass like below and use that any of function or data member in any of class.

创建一个ClassicSingleton.java如下所示的类,并在任何类中使用任何函数或数据成员。

How to make Singleton Class ?

如何制作单例类?

public class ClassicSingleton {

   private static ClassicSingleton instance = null;
   public ArrayList<String> name = new ArrayList<String>(); // Member

   protected ClassicSingleton() {
      // Exists only to defeat instantiation.
   }
   public static ClassicSingleton getInstance() {
      if(instance == null) {
         instance = new ClassicSingleton();
      }
      return instance;
   }
   public String getName()
   {
   String myName="Chintan Khetiya";
   return myName;
   }

   public ArrayList<String> getNameformarray() {
     name.add("Android");
     name.add("IPhone");
     name.add("Windows");
     return name;

    }

}

How to use function and member of the Singleton class ?

如何使用 Singleton 类的函数和成员?

ClassicSingleton CS= new ClassicSingleton();
CS.getInstance();
String myName=CS.getname(); // Output will be >> Chintan Khetiya
String like=CS.getNameformarray().get(1); // Output will be >> Android

same way you can use the data member here as publicly by static reference of object.

同样,您可以通过对象的静态引用在此处公开使用数据成员。

This is best ans stranded way to use.

这是最好的 ans 搁浅使用方式。

回答by Renjith

Try this solution

试试这个解决方案

  • Make a BaseActivityextending Activity
  • Extend your other activities to BaseActivity
  • Create DataPackarray instance in BaseActivity
  • When saving DataPack details, save it in BaseActivity.
  • 做一个BaseActivity延伸Activity
  • 将您的其他活动扩展到 BaseActivity
  • 在中创建DataPack数组实例BaseActivity
  • 保存 DataPack 详细信息时,将其保存在BaseActivity.

回答by amalBit

Android has a special class called the Application class. If you declare any variable there it can be accessed through out your application. Its like a global singleton.

Android 有一个特殊的类,称为 Application 类。如果您在那里声明任何变量,则可以通过您的应用程序访问它。它就像一个全局单例。

  public class DataPack {
  int fNumber;
  int dateTime;
  int Year, fMonth, fDay;
  int fTimeHours, fTimeMin, fTimeSec;
  int fSize;
  char[] name = new char[18];
  char[] surname = new char[18];
  }
  public class A extends Application 
  {     
   DataPack[] mDataPack = new DataPack[50];
  } 

Now go to manifest and make the following change:

现在转到清单并进行以下更改:

   <application android:icon="@drawable/icon" android:label="@string/app_name"
android:name="com.yourAppName.DataPack">

Then you can go to any activity and use this global singleton like:

然后你可以去任何活动并使用这个全局单例,例如:

  DataPack pack = (DataPack)getApplication();
  //get the array using <pack.mDataPack> in a loop.

Advantages of using a singleton: *The application state remains the same even if there is a change in screen orientation.

使用单例的优点: *即使屏幕方向发生变化,应用程序状态也保持不变。