递归,迭代,什么都行,帮忙实现下这个方法

diaobiyong 2009-02-18 03:36:55
代码:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class StringTest {

/**
* 测试
* @param args
*/
public static void main(String[] args) {
String value = "";
Map map = new LinkedHashMap();
for(int i = 0; i < 3 ;i++){
List list = new ArrayList();
value = getValue(i);
for(int j = 0 ; j < 3; j++){
Map vm = new LinkedHashMap();
vm.put("name", value+j);
list.add(vm);
}
map.put("key"+i,list);
}
List returnList = getControlFormulaShow(map);
}

/**
*
* @param i
* @return
*/
public static String getValue(int i){
String returnValue = "";
switch(i){
case 0:
returnValue = "A";
break;
case 1:
returnValue = "B";
break;
case 2:
returnValue = "C";
break;
default:
returnValue = "";
break;
}
return returnValue;
}
/**
*
* @param map
* @return
*/
public static List getControlFormulaShow(Map map){
List returnList = new ArrayList();
Iterator it = map.keySet().iterator();
int colNum= 0;
int f = 1;
int preSize = 1;
while(it.hasNext()){
String key = (String) it.next();
List list = (List)map.get(key);
f = f * list.size();
colNum = colNum + f;
for(int i = 0; i < preSize ;i++){
for(int j = 0;j < list.size();j++){
//这里怎么插入?
returnList.add(returnList.size(), list.get(j));
}
}
preSize = list.size();
}
return returnList;
}
}





实现getControlFormulaShow这个方法
参数map已初始好,数据为:
{key0=[{name=A0}, {name=A1}, {name=A2}], key1=[{name=B0}, {name=B1}, {name=B2}], key2=[{name=C0}, {name=C1}, {name=C2}]}


现要通过getControlFormulaShow方法把数据按这样的顺序放入到一个list中

{name=A0}
{name=B0}
{name=C0}
{name=C1}
{name=C2}
{name=B1}
{name=C0}
{name=C1}
{name=C2}
{name=B2}
{name=C0}
{name=C1}
{name=C2}
{name=A1}
{name=B0}
{name=C0}
{name=C1}
{name=C2}
{name=B1}
{name=C0}
{name=C1}
{name=C2}
{name=B2}
{name=C0}
{name=C1}
{name=C2}
{name=A2}
{name=B0}
{name=C0}
{name=C1}
{name=C2}
{name=B1}
{name=C0}
{name=C1}
{name=C2}
{name=B2}
{name=C0}
{name=C1}
{name=C2}
工39个数据。。。




...全文
103 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
diaobiyong 2009-02-18
  • 打赏
  • 举报
回复
map转数组,但是我不能改变map里list每一个值的数据类型
diaobiyong 2009-02-18
  • 打赏
  • 举报
回复
map和数组本来就不一样,你迭代index的话,数组是也已的,那map怎么取呢
diaobiyong 2009-02-18
  • 打赏
  • 举报
回复
我map里list有几个不是死的,数据我只是模拟了下3个而已
nine_suns99 2009-02-18
  • 打赏
  • 举报
回复

/**
* @param map
* @return
*/
public static List getControlFormulaShow(Map map)
{
List returnList = new ArrayList();
Iterator it = map.entrySet().iterator();
List tempList = new ArrayList();
for (int i = 0; i < map.size(); i++)
{
tempList.add(((Entry)it.next()).getValue());
}

for(int k = 0; k < map.size(); k++)
{
returnList.add(((List)tempList.get(0)).get(k));
for (int i = 0; i < ((List)tempList.get(1)).size(); i++)
{
returnList.add(((List)tempList.get(1)).get(i));
for (int j = 0; j < ((List)tempList.get(2)).size(); j++)
{
returnList.add(((List)tempList.get(2)).get(j));
}
}
}
return returnList;
}
ghostkngiht 2009-02-18
  • 打赏
  • 举报
回复

package com.test;

import java.util.ArrayList;
import java.util.List;

public class CList {

public static void main(String[] arg) {
CList c = new CList();
List<String> list = new ArrayList<String>();
c.set(list, c.array, 0);
for (String l : list) {
System.out.println(l);
}
}

public String[][] array = new String[][] {
{"name=A0", "name=A1", "name=A2"},
{"name=B0", "name=B1", "name=B2"},
{"name=C0", "name=C1", "name=C2"},
{"name=D0", "name=D1", "name=D2"}
};

private void set(List<String> list, String[][] array, int index){
for (int i = 0; i < array[index].length; i++) {
String blank = "";
for (int j = 0; j < index; j++) {
blank += " ";
}
list.add(blank + array[index][i]);
if (index < array.length - 1) {
this.set(list, array, index + 1);
}
}
}
}


map到数组的转换自己写吧
diaobiyong 2009-02-18
  • 打赏
  • 举报
回复
不要帖子沉掉啊,达人们出来看看啊

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧