遍历一个数组套,在获取当前数组里的一个数组的值
时间: 2024-05-07 12:15:59 浏览: 13
可以通过双重循环来遍历一个数组套,然后在内层循环中获取当前数组里的一个数组的值。具体代码如下:
```
int[][] array = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
int value = array[i][j];
System.out.println("当前数组的值为:" + value);
}
}
```
上面的代码会依次输出数组里的每个值:
```
当前数组的值为:1
当前数组的值为:2
当前数组的值为:3
当前数组的值为:4
当前数组的值为:5
当前数组的值为:6
当前数组的值为:7
当前数组的值为:8
当前数组的值为:9
```
相关问题
遍历一个数组,取出值放在列表里怎么坐
你可以使用循环来遍历整个数组,然后将每个元素添加到一个新的列表中。以下是一个示例代码,假设你要遍历的数组名为 `arr`:
```python
new_list = []
for item in arr:
new_list.append(item)
```
这将遍历 `arr` 中的每个元素,并将其添加到 `new_list` 中。然后你可以使用 `new_list` 进行后续的操作。
qt5怎么遍历一个数组变量的值是否在一个结构体数组中
如果要遍历一个数组变量的值是否在一个结构体数组中,可以使用循环遍历数组变量,然后在循环中使用foreach循环和结构体数组的成员变量进行比较。
例如,以下代码定义一个包含多个学生信息的结构体数组和一个整型数组变量,然后遍历整型数组中的每个元素,判断是否有元素的值等于学生信息结构体数组中某个学生的年龄:
```
struct Student {
QString name;
int age;
QString gender;
};
int ageList[] = {18, 20, 22};
Student studentList[] = {
{"Tom", 18, "Male"},
{"Mary", 19, "Female"},
{"John", 20, "Male"},
{"Lucy", 21, "Female"},
{"David", 22, "Male"}
};
for(int i = 0; i < sizeof(ageList) / sizeof(int); i++) {
bool found = false;
int age = ageList[i];
foreach(Student stu, studentList) {
if(stu.age == age) {
found = true;
qDebug() << "Found student with age" << age << ":" << stu.name;
break;
}
}
if(!found) {
qDebug() << "No student found with age" << age;
}
}
```
以上代码中,通过循环遍历整型数组ageList中的每个元素,然后在循环中使用foreach循环遍历学生信息结构体数组studentList中的每个元素,比较每个学生信息的年龄是否等于当前整型数组元素的值,如果相等则输出该学生的信息。
这样就可以遍历一个数组变量的值是否在一个结构体数组中了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)