首页 > 解决方案 > int[] 不能转换为 int[][]

问题描述

我有一个这样的 int[][] 建立:

public static final int[][] answers

但是我在迭代它时遇到了问题。我有这个:

for(int[][] s : answers)

我收到一条错误消息,提示“不兼容的类型:int[] 无法转换为 int[][]”。它们都是 int[][] 所以我很困惑为什么会这样。

标签: java

解决方案


int[][]中,二维数组的每个元素都是一个整数数组 ( int[])。

在您的 for 循环中,您希望遍历 中的每个元素sanswers因此每个元素s都是类型int[]

所以,你需要的是for(int[] s : answers),而不是for(int[][] s : answers)


推荐阅读