Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在同一个循环中同时迭代多个数组_Java_Spring Boot_Spring Mvc_Thymeleaf - Fatal编程技术网

Java 如何在同一个循环中同时迭代多个数组

Java 如何在同一个循环中同时迭代多个数组,java,spring-boot,spring-mvc,thymeleaf,Java,Spring Boot,Spring Mvc,Thymeleaf,我不熟悉thymeleaf,我想在同一div.row中显示3个不同数组中的3个值,具有相同的索引。我尝试了几种方法,但一次只能迭代一个数组,没有错误,下面是我的控制器端: public String index(Model model) { String[] table0 = {"0","1","2","3"} String[] table1 = {"14","21","25","75"} String[] table2 = {"7","63","57","87"} mod

我不熟悉thymeleaf,我想在同一div.row中显示3个不同数组中的3个值,具有相同的索引。我尝试了几种方法,但一次只能迭代一个数组,没有错误,下面是我的控制器端:

public String  index(Model model) {

  String[] table0 = {"0","1","2","3"}
  String[] table1 = {"14","21","25","75"}
  String[] table2 = {"7","63","57","87"}

  model.addAttribute("table0", table0;
  model.addAttribute("table1", table1);
  model.addAttribute("table2", table2);

  return "index";
}
在html文件中,table0是第一个无错误迭代的数组,我不知道如何编辑/改进以下代码以同时显示所有三个数组table0、Table1和Table3:

<div class="row" th:each="v0 : ${tables0}" >
    <div class="cell" th:text="value">
        <!-- Here I could display a value from tables0 -->
    </div>
    <div class="cell"  >
        <!-- Here I need to display the value of tables1 having the same index as v0 -->
    </div>
    <div class="cell"  >
        <!-- Here I need to display the value of tables2 having the same index as v0 -->                        
    </div>
</div>
您可以使用Thymeleaf的iterStat来完成此操作

假设以下输入数据:

String[] table0 = {"0", "1", "2", "3"};
String[] table1 = {"14", "21", "25", "75"};
String[] table2 = {"7", "63", "57", "87"};
您可以使用以下标记:

<div class="row" th:each="val,iterStat  : ${table0}" >
    <div class="cell" th:text="${val}">
    </div>
    <div class="cell" th:text="${table1[iterStat.index]}">
    </div>
    <div class="cell" th:text="${table2[iterStat.index]}">
    </div>
</div>  
相关的html如下所示:

<div class="row">
    <div class="cell">0</div>
    <div class="cell">14</div>
    <div class="cell">7</div>
</div>
<div class="row">
    <div class="cell">1</div>
    <div class="cell">21</div>
    <div class="cell">63</div>
</div>
<div class="row">
    <div class="cell">2</div>
    <div class="cell">25</div>
    <div class="cell">57</div>
</div>
<div class="row">
    <div class="cell">3</div>
    <div class="cell">75</div>
    <div class="cell">87</div>
</div>                
描述了iterStat函数-它基本上跟踪您的迭代。因为您希望每个表都有相同的索引,所以它非常适合您的需要。

您可以使用Thymeleaf的iterStat来实现这一点

假设以下输入数据:

String[] table0 = {"0", "1", "2", "3"};
String[] table1 = {"14", "21", "25", "75"};
String[] table2 = {"7", "63", "57", "87"};
您可以使用以下标记:

<div class="row" th:each="val,iterStat  : ${table0}" >
    <div class="cell" th:text="${val}">
    </div>
    <div class="cell" th:text="${table1[iterStat.index]}">
    </div>
    <div class="cell" th:text="${table2[iterStat.index]}">
    </div>
</div>  
相关的html如下所示:

<div class="row">
    <div class="cell">0</div>
    <div class="cell">14</div>
    <div class="cell">7</div>
</div>
<div class="row">
    <div class="cell">1</div>
    <div class="cell">21</div>
    <div class="cell">63</div>
</div>
<div class="row">
    <div class="cell">2</div>
    <div class="cell">25</div>
    <div class="cell">57</div>
</div>
<div class="row">
    <div class="cell">3</div>
    <div class="cell">75</div>
    <div class="cell">87</div>
</div>                

描述了iterStat函数-它基本上跟踪您的迭代。由于每个表都需要相同的索引,因此非常适合您的需要。

在这里您可以找到您正在搜索的内容

只需在对象之后添加一个var,然后使用index来获取当前的索引值

举例来说:

<div class="row" th:each="v0,iter : ${tables0}" >
    <div class="cell" th:text="value">
        <!-- Here I could display a value from tables0 -->
        <span th:text="${v0}"></span>
    </div>
    <div class="cell"  >
        <span th:text="${table1[iter.index]}"></span>
    </div>
    <div class="cell"  >
        <span th:text="${table2[iter.index]}"></span>                  
    </div>
</div>

在这里你可以找到你正在搜索的东西

只需在对象之后添加一个var,然后使用index来获取当前的索引值

举例来说:

<div class="row" th:each="v0,iter : ${tables0}" >
    <div class="cell" th:text="value">
        <!-- Here I could display a value from tables0 -->
        <span th:text="${v0}"></span>
    </div>
    <div class="cell"  >
        <span th:text="${table1[iter.index]}"></span>
    </div>
    <div class="cell"  >
        <span th:text="${table2[iter.index]}"></span>                  
    </div>
</div>

您需要将逻辑拆分为线程。如果您只有一个数组,并且该数组中的每个索引都有一个包含三个值的对象,那么问题就会简单得多。@Gimby这是唯一的方法吗?@Splaten我该怎么做?@MiraSoft,您在帖子中提到Java,google Java thread tutorial或转到此处:您需要将逻辑拆分为线程。如果您只有一个数组,并且该数组中的每个索引都有一个包含三个值的对象,那么您的问题就会简单得多。@Gimby这是唯一的方法吗?@Splaten我该怎么做?@MiraSoft,您在帖子中提到Java,谷歌Java线程教程或点击此处: