IndexOf两个HashSet字符串 - 共享首选项

时间:2018-02-08 23:25:26

标签: java android arrays

您好我有两个字符串集,我想获得当前存在的索引:

我想在第一组的第二组中使用相同的索引,所以我可以在其他地方使用这些字符串,所以如果我拥有的标记是" VatU5fy1U4cgTc3uBBiGX7G7kuT2" 那么相关的出生日期将是" 2011年2月8日" 。 (见下面的输出)。

现在我理解集合是无序的,并且索引不能被使用,因为我使用的是共享首选项中的方法,例如getStringSet,它仅检索集合而不是数组中的字符串。

我已经尝试将该集转换为数组,但每次我使用索引时我总是将-1返回给我,其中无法找到该字符串! :(

这是我当前的代码(非常标准,它完美地工作),对于日期的共享首选项,对于另一个令牌集是相同的:

    final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    final SharedPreferences.Editor editor = sharedPreferences.edit();

    Set<String> myDates = sharedPreferences.getStringSet("myDates", new HashSet<String>());

    String dates = sharedPreferences.getString("savedDate", "");
// Add the new value.
    myDates.add(dates);


// Save the list.
    editor.putStringSet("myDates", myDates);
    editor.commit();


    Log.w(TAG, "Onexit value of dates in array: " + String.valueOf(myDates));

输出:

Onexit value of tokens in array: [Ztg4GOqeucd6CqNAEokUe7eRHhY2, VatU5fy1U4cgTc3uBBiGX7G7kuT2]
Onexit value of dates in array: ["8th February 2012", "8th February 2011"]

以下是我尝试将这些列表转换为数组并将其视为数组的解决方案:

**//Attempt 1**

    Set<String> myDates = sharedPreferences.getStringSet("myDates", new 
    HashSet<String>());

    List<String> myDatesList = new ArrayList <String> (myDates);
    int i = Arrays.asList(myDatesList).indexOf(dates);        

    myDates.add(dates);

    List<String> myDatesList = new ArrayList <String> (myDates);

    editor.putStringSet("myDates", myDates);
    editor.commit();

int i = Arrays.asList(myDatesList).indexOf(dates);

返回-1 ^

**//Attempt 2**

List<String> myDatesList = new ArrayList <String> (myDates);
String datesString = TextUtils.join(";", myDatesList);
editor.putString("newdates", datesString);
editor.apply();


final String datesString = sharedPreferences.getString("newdates", "");
String dates = sharedPreferences.getString("savedDate", "");
List<String> myDatesList = new ArrayList<>();
editor.commit();
if (!datesString.isEmpty()){
    myDatesList = new ArrayList<>(Arrays.asList(datesString.split(";")));
}

String datesString = TextUtils.join(";", myDatesList);
editor.putString("newdates", datesString);
editor.apply();
int i = Arrays.asList(datesString).indexOf(dates);
**returns -1** ^

**回答答案 - 更新**

回应评论:(我改变了日志)

        int i = myDatesList.indexOf(datesString);
        Log.w(TAG, "My dates list: " + dates + " and datesString is: " + datesString + i);

给我:

My dates list: "9th August 2017" and datesString is: "9th August 2017"0
My dates list: "9th February 2011" and datesString is: "9th August 2017";"9th February 2011"-1

为什么它第二次给我-1而不是1?

1 个答案:

答案 0 :(得分:0)

List<T>接受varargs,不是用于制作T类型的副本,而是用于创建// Contains each element from myDates List<String> myDatesList = new ArrayList<String>(myDates); ,其中Arrays.asList(myDatesList).indexOf(dates)是传递的对象类型作为参数。

当你有:

时发生的事情
Arrays.asList()

并致电List<List<String>>// This is what's actually being returned: List<List<String>> result = Arrays.asList(myDatesList); 调用返回的内容是indexOf()的实例。

String

在这种情况下,使用String类型调用List<String>将永远不会找到匹配项(因为String永远不会等于List<List<String>> may not contain objects of type String。事实上,这List<String>参数应在IDE中显示为警告,表示myDatesList

您已经拥有Arrays.asList()List),因此无需调用myDatesList.indexOf(dateString),因为它旨在创建传入类型的<?php ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); include 'dbconnection.php'; $first = $_POST['fname']; $last = $_POST['lname']; $email = $_POST['email']; $password = $_POST['password']; $query = mysql_query("SELECT * FROM users WHERE Email='$email'"); if(mysql_num_rows($query) > 0){ echo "That account already exists!"; }else{ $query = mysqli_query("INSERT INTO users (FirstName, LastName,Email,Password) values ('$first','$last','$email','$password')"); header("location: signedup.php"); } ?> 。调用.click()来获取实际索引。