1.在形如:"abc${dd}fdf;${gfd}lk${tts$}"的字符窜中提取${}中的数据,其中数据必须为数字或字母的组合
寻找以"${"开头的位置,再寻找与之匹配的"}".如此循环
String start = "${";
String end = "}";
List<String> inputList = new LinkedList<String>();
int i = str.indexOf("${");
// 第一个"}"的位置
while (i != -1)
{
int j = str.indexOf('}', i + 3);
if (j == -1)
{
break;
}
String input = str.substring(i + 2, j);
if (input.matches("[0-9a-zA-Z]+"))
{
inputs.add(input);
}
i = str.indexOf("${", i + 1);
}
2.广义查找算法
/**
* 获得所有非根菜单的菜单节点集合
* @return menus
*/
public List<MenuModel> getSubMenus()
{
LinkedList<AbstractModel> models = new LinkedList<AbstractModel>();
List<MenuModel> menus = new LinkedList<MenuModel>();
models.add(this);
while (!models.isEmpty())
{
//从队列中弹出元素
AbstractModel model = models.removeFirst();
//将可能含有menu节点的子元素加入队列
Object[] childs = model.getChildren();
for (Object obj : childs)
{
if (obj instanceof MenuRootModel)
{
models.addLast((MenuRootModel)obj);
}
else if (obj instanceof MenuModel)
{
models.addLast((MenuModel)obj);
}
}
if (model instanceof MenuModel)
{
MenuModel menuModel = (MenuModel)model;
menus.add(menuModel);
}
}
return menus;
}
寻找以"${"开头的位置,再寻找与之匹配的"}".如此循环
String start = "${";
String end = "}";
List<String> inputList = new LinkedList<String>();
int i = str.indexOf("${");
// 第一个"}"的位置
while (i != -1)
{
int j = str.indexOf('}', i + 3);
if (j == -1)
{
break;
}
String input = str.substring(i + 2, j);
if (input.matches("[0-9a-zA-Z]+"))
{
inputs.add(input);
}
i = str.indexOf("${", i + 1);
}
2.广义查找算法
/**
* 获得所有非根菜单的菜单节点集合
* @return menus
*/
public List<MenuModel> getSubMenus()
{
LinkedList<AbstractModel> models = new LinkedList<AbstractModel>();
List<MenuModel> menus = new LinkedList<MenuModel>();
models.add(this);
while (!models.isEmpty())
{
//从队列中弹出元素
AbstractModel model = models.removeFirst();
//将可能含有menu节点的子元素加入队列
Object[] childs = model.getChildren();
for (Object obj : childs)
{
if (obj instanceof MenuRootModel)
{
models.addLast((MenuRootModel)obj);
}
else if (obj instanceof MenuModel)
{
models.addLast((MenuModel)obj);
}
}
if (model instanceof MenuModel)
{
MenuModel menuModel = (MenuModel)model;
menus.add(menuModel);
}
}
return menus;
}