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?

2
  • 1
    You can do this in one loop of the array list, using get/set. Use a standard for loop, not the enhanced one. Commented Jan 27, 2013 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. Commented Jan 27, 2013 at 14:18

2 Answers 2

1

If I understand you correctly -

int index =0;
if((index = arrayList.get(element))>=0)
    arrayList.set(index,replaceElement);
Sign up to request clarification or add additional context in comments.

1 Comment

Got it working with what @Perception advised. Will try your way out now to see if it does what I need. Thanks =D
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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.