如何保存/加载BigInteger数组

时间:2015-04-09 10:25:56

标签: java android casting sharedpreferences biginteger

我想在SharedPreferences中保存/加载BigInteger数组。 怎么办呢?

例如,对于以下数组:

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

3 个答案:

答案 0 :(得分:1)

bigInts

中考虑BigInteger[]是您想要的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]);
}

答案 1 :(得分:1)

使用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);
    }
}

在gradle add依赖项中添加Gson:

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

答案 2 :(得分:0)

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

这是您的保存偏好方法

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

现在回到你的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());
    }

这是你的loadPreference方法

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

注意:您可以根据需要更改分隔符,但更喜欢使用字符数组。相应地更改分隔符长度

试试这个,让我知道它是否有效