-2

I am using an ArrayList in my application.

I would like to know the exact procedure to initialize my ArrayList from a Singleton class.
The data will be used in some other Activities.

Can anybody help to know about Singleton class?

3
  • Please, add some code and further explanation to help us understand the problem. Commented Oct 20, 2016 at 11:31
  • What efforts you have done Commented Mar 26, 2020 at 16:30
  • I hope you were able to turn in your homework on time! Commented Aug 23, 2021 at 16:38

3 Answers 3

10

Here is how to create your singleton class :

    public class YourSingleton  {  

        private static YourSingleton mInstance;
        private ArrayList<String> list = null;

        public static YourSingleton getInstance() {
            if(mInstance == null)
                mInstance = new YourSingleton();

            return mInstance;
        }

        private YourSingleton() {
          list = new ArrayList<String>();
        }
        // retrieve array from anywhere
        public ArrayList<String> getArray() {
         return this.list;
        }
        //Add element to array
        public void addToArray(String value) {
         list.add(value);
        }
}

Anywhere you need to call your arrayList just do :

YourSingleton.getInstance().getArray(); 

To add elements to array use :

 YourSingleton.getInstance().addToArray("first value"); 

or

YourSingleton.getInstance().getArray().add("any value"); 
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks @Anto.. and one more thing is can I use multiple arraylist in same singleton class
yes you can, and create differents methods to call correct array. In this exemple you have getArray(). You could create getFirstArray(), getSecondArray() etc... Just add your others array in class declaration and initialize them in constructor just like i did for 'list'
Got an Idea.. Very kind of you@Anto Lemme checkout and let u know
How to set data in list from singleton class
edited my post, you need to create a method that will affect the array
|
1

Please look at the following wikipedia-artikle:

https://en.wikipedia.org/wiki/Singleton_pattern

But keep in mind that singletons are 'global state' and make your sourcecode harder to test. There are lot of people saying: "singletons are evil'

Comments

0

I think you need something like this.

public class SingletonClass {

    private static ArrayList<String> strArray;
    private static SingletonClass singleton;

    private SingletonClass(){}

    public static synchronized SingletonClass getConnectionInstance(ArrayList<String> strArray){        
        if (singleton == null) {
             singleton = new SingletonClass();
        }
        this.strArray = strArray;
        return singleton;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.