Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.替换(列表(x),“不”和“x27”;t工作Java_Java_Arrays_Arraylist - Fatal编程技术网

.替换(列表(x),“不”和“x27”;t工作Java

.替换(列表(x),“不”和“x27”;t工作Java,java,arrays,arraylist,Java,Arrays,Arraylist,我写了代码: List<String> Names = new ArrayList<String>(); List<Double> Prices = new ArrayList<Double>(); List<String> Dates = new ArrayList<String>(); List<String> Hours = new ArrayList<String>(); List<

我写了代码:

List<String> Names = new ArrayList<String>();
List<Double> Prices = new ArrayList<Double>();
List<String> Dates =  new ArrayList<String>();
List<String> Hours =  new ArrayList<String>();
List<String> iDs =  new ArrayList<String>();
SimpleAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView resultsListView = (ListView) findViewById(R.id.listMyReceipts);
    SearchView m = (SearchView) findViewById(R.id.action_search);

    HashMap<String, String> NamePrice = new HashMap<>();

    TextView test = (TextView) findViewById(R.id.test);

    Names.add("Starbucks"); Prices.add(11.99); Dates.add("01/01/2017"); Hours.add("9:33");
    Names.add("Apple Store"); Prices.add(500.00); Dates.add("01/01/2017"); Hours.add("9:30");
    Names.add("Esselunga"); Prices.add(135.67); Dates.add("01/01/2017"); Hours.add("11:51");
    Names.add("Mediaworld"); Prices.add(19.98); Dates.add("01/01/2017"); Hours.add("12:03");
    Names.add("Starbucks"); Prices.add(11.99); Dates.add("01/01/2017"); Hours.add("12:47");

    for (int i = 0; i < Names.size(); i++) {
        iDs.add(";+@:" + i + ":@+;");
        NamePrice.put(iDs.get(i) + Names.get(i), "    " + Prices.get(i).toString() + "€" + "  -  " + Dates.get(i) + "  " + Hours.get(i));
    }



    List<HashMap<String, String>> listItems = new ArrayList<>();
    adapter = new SimpleAdapter(this, listItems, R.layout.list_item,
            new String[] {"First", "Second"},
            new int[] {R.id.listitemTitle, R.id.listitemSubItem});

    //Integer x = 0;
    //int x = -1;
    Iterator it = NamePrice.entrySet().iterator();
    for (int i = 0; i < iDs.size(); i++) {
        HashMap<String, String> resultMap = new HashMap<>();
        Map.Entry pair = (Map.Entry) it.next();
        resultMap.put("First", pair.getKey().toString().replace(iDs.get(i), ""));
        resultMap.put("Second", pair.getValue().toString());
        listItems.add(resultMap);
    }
    //test.setText(IDs.get(x - 1));

    resultsListView.setAdapter(adapter);

}
它将正确替换元素0。
输出为:NAME1 12.99;+@:1:@+;NAME2 0.99

您没有按预期的顺序获得
NamePrice
的条目。我在第二个
for
循环中插入了一个
System.out.println()
,并按以下顺序获得了输出:

;+@:3:@+;Mediaworld
;+@:0:@+;Starbucks
;+@:4:@+;Starbucks
;+@:1:@+;Apple Store
;+@:2:@+;Esselunga
因此,您正在尝试替换
;+@:0:@+;
;+@:3:@+;Mediaworld
等等。这不能取代任何东西。原因是
NamePrice
是一个
HashMap
HashMap
的迭代器不会以任何特定顺序返回条目,特别是不会以插入顺序返回条目

您可以使用
LinkedHashMap

    LinkedHashMap<String, String> NamePrice = new LinkedHashMap<>();
LinkedHashMap NamePrice=new LinkedHashMap();

它的迭代器按照条目的创建顺序返回条目。现在替换工作正常了。

因为
replace
提供了新的字符串对象,旧的字符串对象没有修改,所以我该怎么办@PavneetSinghwait,你的意思是,在循环内部,使用索引,你的替换不起作用,似乎我用另一种方式解释了你的问题是的,我的意思是,在循环中,使用索引,我的替换不起作用。但是,如果我使用,例如,0作为索引,它会正确地替换元素0@PavNeetsingQuestions寻求调试帮助(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请看:。只有一个词:真棒
    LinkedHashMap<String, String> NamePrice = new LinkedHashMap<>();