How to save/load BigInteger array

2020-05-09 22:52发布

问题:

I want to save/load a BigInteger array into/from the SharedPreferences. How can it be done?

For example for the following array:

private BigInteger[] dataCreatedTimes = new BigInteger[20];

回答1:

Using Gson you can convert to a json String and back, which then of course makes it trivial to save in preferences:

import com.google.gson.Gson;

import java.math.BigInteger;

public final class BigIntegerArrayJson {    
    private BigIntegerArrayJson(){}

    public static String toJson(BigInteger[] array) {
        return new Gson().toJson(array);
    }

    public static BigInteger[] fromJson(String json) {
        return new Gson().fromJson(json, BigInteger[].class);
    }
}

To add Gson in gradle add dependency:

dependencies {
    compile 'com.google.code.gson:gson:1.7.2'
}


回答2:

Consider bigInts is the BigInteger[] you want from Preference:

BigInteger[] bigInts = new BigInteger[n];
Set<String> set = new HashSet<String>();

for(BigInteger bigInt : bigInts) {
    set.add(bigInt.toString());
}

//store into Preference
SharedPreference.Editor editor = getSharedPreference(getPackageName(), Context.MODE_PRIVATE).edit();
editor.putStringSet("bigints", set);

//get BitInteger[] from Preference
SharedPreference pref = getSharedPreference(getPackageName(), Context.MODE_PRIVATE);

Set<String> set = pref.getStringSet("bigints", new HashSet<String>());
int count = set.size();

String[] strs = new String[count];
BigInteger[] bigInts= new BigInteger[count];
set.toArray(strs);

for(int i = 0; i < count; i++) {
    bitInts[i] = new BigInteger(strs[i]);
}


回答3:

    final String DELIMITER = "BOND";
    final int DELIMITER_LENGTH = 4;
    String str = "";
    BigInteger[] integer = new BigInteger[50];

    for(int i = 0; i < integer.length ; i++) {
        str += integer[i].toString() + DELIMITER;
    }

 savePreference("your_key", str);

and here's your save preference method

public static void savePreference(String iName, String iValue) {
        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(
                MainApplication.getsApplicationContext()).edit();
        // Check that passed key is not null
        if (iName != null && iValue != null) {
            editor.putString(iName, iValue);
            editor.commit();
        }
    }

Now to get back your BigInteger Values ,

String str = loadPreference("your_key");
ArrayList<BigInteger> myBigInt = new ArrayList<>();


        while(str != null){
            int subStringLastIndex = 0;
            if(str.contains(DELIMITER) && str.length() != DELIMITER_LENGTH){
              subStringLastIndex = str.indexOf(DELIMITER.charAt(0));
            myBigInt.add(new BigInteger(str.substring(0, subStringLastIndex)));
            str = str.substring(subStringLastIndex + 4);

            }else{
                str = null;
            }
        }

    for(int i = 0; i < myBigInt.size(); i++){
        Log.d(TAG, myBigInt.get(i).toString());
    }

Here's your loadPreference Method

public static String loadPreference(String iName) {
            return PreferenceManager.getDefaultSharedPreferences(MainApplication.getsApplicationContext()).
                    getString(iName,null);
        }

Note : You can change delimiter what ever you want, but prefer to take character array. Change Delimiter length accordingly

Try this and let me know if it works