简体   繁体   中英

Replacing an ArrayList item with a Linkedhashmap VALUE

I have a Linkedhashmap called numericValueMap that has the parameters String,String :

{a=1,b=1,c=2,d=3,e=5}

And I have an Arraylist called equation:

{ e, -, a, +, b, -, c, *, d, +, b}

What I want to do is replace the items in the Arraylist with the correct Values from the Linkedhashmap. Here is my attampt so far:

for (final String key: numericValueMap.keySet()) {
for (int i = 0, n = equation.size(); i < n; i++) {
String algebraItems = (String) equation.get(i);

if(algebraItems.contains(key)) {
    String newNum = algebraItems.replace(algebraItems, ?);
    equation.set(i,newNum);

  }
 }
 }

The code works up until and including the point where it compares the Arraylist to the corresponding Linkedhashmap Key. However with my current knowledge, I am unable to replace the Arraylist item with the correct Value. Any ideas on what code I must use?

If I understand you correctly -

int index =0;
if((index = arrayList.get(element))>=0)
    arrayList.set(index,replaceElement);

Works by iterating through the LinkedHashMap and gaining access to each items KEY and VALUE like so:

for (Iterator<Entry<String, String>> it = numericValueMap.entrySet().iterator(); it.hasNext();) {
            Entry<String, String> e = it.next();
            String key = e.getKey();
            String value = e.getValue();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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