1

Given an ArrayList of the form:

{ "(", "2", "+", "4", ")", "/", "2"}

I want to access all of the items between the brackets and then run a method on those items.

To do this I will have to say something like:

while(arrayList.contains("(")&&arrayList.contains(")")){
    // access the items between the brackets
}

The brackets won't always be in the same position and will have varying numbers of items in between them. How will I go about accessing those items?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Digitalwolf
  • 447
  • 2
  • 9
  • 20
  • 2
    the simpliest approach would be to iterate over all the elements in a loop and check if opening bracket was present, process those elements (whatever you need to do there) and then end processing on closing bracket. However depending on the problem you're trying to solve, there might be some more optimal way (possibly some more optimal data structure to hold your data) – Peter Butkovic Jan 25 '13 at 08:09
  • *"..run a method on those items."* You mean like [`ScriptEngine.eval(String)`](http://docs.oracle.com/javase/7/docs/api/javax/script/ScriptEngine.html#eval%28java.lang.String%29)? – Andrew Thompson Jan 25 '13 at 08:10
  • 1
    Try something. then come back if you're stuck, and show us what you tried. – JB Nizet Jan 25 '13 at 08:12
  • Can you have more than one level of parentheses? – NPE Jan 25 '13 at 08:12
  • Yes it is possible to have an infinite number of levels of parentheses or have multiple separate parentheses. – Digitalwolf Jan 25 '13 at 08:26
  • Will attempt it now that I have some ideas on what I can do. will let you know if I get stuck. – Digitalwolf Jan 25 '13 at 08:37

3 Answers3

4

You need to get indexes for the brackets in your arraylist. For the data structure you use I think should have looked at javadoc to get information about what you can do using it. ArrayList.contains() is a useful method for ArrayList, however, ArrayList.indexOf() would be more useful for this situation.

public int indexOf(Object o)

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.

Using this method you can get the indexes of two consecutive opening-closing brackets, of course, if they exist. After you get the indexes, you can iterate between them. It is kinda parsing work, so you might get your hands dirty by trying to implement some recursive methods.Ex., { "(", "(","2", "+", "4", ")", "/", "2", ")"}. For nested statements like this, you should research more.

What you should need to know is tree for complex statements. I strongly advise you to check tree data structure.

Edit : You can also find numerous stack implementations for this problem. Keywords: stack expression parser algorithm.

3

Use something like this

String exp=/*ArrayList.toString()*/
exp=exp.replace(",","");
exp=exp.replace("[","");

After you get the expression

you can use the built-in Javascript engine.

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;

public class Test {
  public static void main(String[] args) throws Exception{
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("JavaScript");
    String foo = "40+2";
    System.out.println(engine.eval(foo));
    } 
}

reference : stackoverflow

Community
  • 1
  • 1
Bhavik Shah
  • 5,125
  • 3
  • 23
  • 40
3

You could do something like this:

ArrayList<String> list; // The list you want to process
for (int i = list.indexOf("(") + 1; i < list.indexOf(")"); i++) {
    // Do something with list.get(i)
}

This will only work with exactly one occurence of "(" and ")", but you can quite easily modify the code to your need.

  • Will test it out. But it should be relatively easy to get it to search for multiple occurrences. Just need to be able to do one now for proof of concept. – Digitalwolf Jan 25 '13 at 08:33