在循环中创建列表

时间:2017-06-05 13:04:10

标签: java arrays loops dynamic

有没有办法创建一个创建新List

的循环
def ocrl(index, src, maxOffset, maxLength):
    """
    Returns starting position in source before index from where the max runlength is detected.
    """
    size = len(src)
    if index>=size:
        return (-1, 0)
    startPos = index - 1 # max(0, index-maxOffset)
    stopPos = max(0, index-maxOffset)
    runs = {}
    while(startPos >= stopPos):
        currRun = 0
        pos = startPos
        while src[pos] == src[index+currRun]:
            currRun += 1
            pos += 1
            if currRun == maxLength:
                return (startPos, maxLength) #found best possible run
            if (pos >= size) or ((index+currRun) >= size):
                break
        if (currRun > 0) and (currRun not in runs.keys()):
            runs[currRun] = startPos
        startPos -= 1

    if not runs:
        return (-1, 0)
    else:
        # Return the index from where the longest run was found
        return (runs[max(runs.keys())], max(runs.keys()))

有没有办法创建一个循环来检查数字是否可以被整除? 如果我打算使用这个

static Map<Integer, List> dataMap = new HashMap<>();
public static void dec_hex() {

        // Map<Integer, Integer> dataMap = new HashMap<>();
        // dataMap.put(0, 0);
        List<Integer> ls2 = new ArrayList<Integer>();
        List<Integer> ls3 = new ArrayList<Integer>();
        List<Integer> ls4 = new ArrayList<Integer>();
        List<Integer> ls5 = new ArrayList<Integer>();
        List<Integer> ls6 = new ArrayList<Integer>();
        List<Integer> ls7 = new ArrayList<Integer>();
        List<Integer> ls8 = new ArrayList<Integer>();
        List<Integer> ls9 = new ArrayList<Integer>();
        List<Integer> ls10 = new ArrayList<Integer>();

        System.out.println("Liczby");
        for (int i = 0; i < 201; i++) {
            String hex = Integer.toHexString(i);
            System.out.println("DEC: " + i + " HEX: " + hex);
            //for (int a = 2; a<11 ; a++){
            if (i % 2 == 0) {
                ls2.add(i);
                //myList.add(i);
                dataMap.put(2, ls2);
            }
            if (i % 3 == 0) {
                ls3.add(i);
                //myList.add(i);
                dataMap.put(3, ls3);
            }

            if (i % 4 == 0) {
                ls4.add(i);
                //myList.add(i);
                dataMap.put(4, ls4);
            }
            if (i % 5 == 0) {
                ls5.add(i);
                //myList.add(i);
                dataMap.put(5, ls5);
            }
            if (i % 6 == 0) {
                ls6.add(i);
                //myList.add(i);
                dataMap.put(6, ls6);
            }
            if (i % 7 == 0) {
                ls7.add(i);
                //myList.add(i);
                dataMap.put(7, ls7);
            }
            if (i % 8 == 0) {
                ls8.add(i);
            //  myList.add(i);
                dataMap.put(8, ls8);
            }
            if (i % 9 == 0) {
                ls9.add(i);
                //myList.add(i);
                dataMap.put(9, ls9);
            }
            if (i % 10 == 0) {
                ls10.add(i);
                //myList.add(i);
                dataMap.put(10, ls10);
            }
        }

        //String list = myList.toString();
        //System.out.println(list);
        dataMap.toString();
        System.out.println(dataMap);

    }

程序将无法正常工作,因为数字会重新开始,因为我使用相同的lsa数组。

3 个答案:

答案 0 :(得分:4)

您可以创建列表列表。

int max = 10;
List<List<String>> listOfLists = new ArrayList<>();
for(int i=0; i<max; i++) {
             //create a new list and add it to listOfLists
    List<String> myList = new ArrayList<>();
    listOfLists.add(myList);
}

希望这会有所帮助。 :)

答案 1 :(得分:1)

是的,当然。这就是地图的用途:你给它一把钥匙,然后它会给你回复价值。

public function parentEvents()
{
    return $this->belongsToMany('App\Event', EventUser::TABLE_NAME, 'user_id', 'event_id');
}

public function childEvents()
{
    return $this->belongsToMany('App\Event', EventUser::TABLE_NAME, 'user_id', 'parent_id');
}

public function events()
{
    $parentEvents = $this->parentEvents;
    $childEvent = $this->childEvents;

    // Merge collections and return single collection.
    return $parentEvents->merge($childEvents);
}

但是由于你的键是从0到9的整数,你也可以使用 Map<Integer, List<Integer>> dataMap = new HashMap<>(); for (int i = 0; i < 10; i++) { dataMap.put(i, new ArrayList<>()); } for (int i = 0; i < 201; i++) { int key = i % 10; List<Integer> list = dataMap.get(key); list.add(i); } 而不是地图。

答案 2 :(得分:0)

您可以通过检查地图是否包含密钥来缩短您的时间,如果没有创建新的列表并添加您的数据:

for (int i = 1; i < 201; i++) {

   for(int j=2;j<11;j++){

      if(i%j==0){

         if(dataMap.containsKey(j){

           dataMap.get(j).add(i);

         }else{

           List<Integer> list = new ArrayList<Integer>();
           list.add(i);
           dataMap.put(j,list);

        }

      }

   }
}