简体   繁体   English

如何保存/加载BigInteger数组

[英]How to save/load BigInteger array

I want to save/load a BigInteger array into/from the SharedPreferences. 我想将BigInteger数组保存/加载到SharedPreferences中。 How can it be done? 如何做呢?

For example for the following array: 例如以下数组:

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

Consider bigInts is the BigInteger[] you want from Preference : 考虑bigInts是您想要从Preference bigIntsBigInteger[]

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]);
}

Using Gson you can convert to a json String and back, which then of course makes it trivial to save in preferences: 使用Gson可以将其转换为json String并返回,然后当然可以轻松保存首选项:

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: 要在gradle中添加Gson,请添加依赖项:

dependencies {
    compile 'com.google.code.gson:gson:1.7.2'
}
    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 , 现在返回您的BigInteger值,

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 这是您的loadPreference方法

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 试试这个,让我知道它是否有效

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

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