0

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?

Digitalwolf
  • 447
  • 2
  • 9
  • 20
  • 1
    You can do this in one loop of the array list, using get/set. Use a standard for loop, not the enhanced one. – Perception Jan 27 '13 at 14:09
  • Got it working thanks. Just had to use an iterator and the get method. Will try and get it to work with set though instead of replace. – Digitalwolf Jan 27 '13 at 14:18

2 Answers2

1

If I understand you correctly -

int index =0;
if((index = arrayList.get(element))>=0)
    arrayList.set(index,replaceElement);
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
  • Got it working with what @Perception advised. Will try your way out now to see if it does what I need. Thanks =D – Digitalwolf Jan 27 '13 at 14:20
0

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();
Digitalwolf
  • 447
  • 2
  • 9
  • 20