list.get()是一个数组 如何遍历
时间: 2024-05-12 12:20:25 浏览: 11
可以使用for循环来遍历list.get()数组,例如:
```
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
for (String element : list) {
System.out.println(element);
}
```
输出:
```
a
b
c
```
也可以使用普通的for循环来遍历,例如:
```
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
for (int i = 0; i < list.size(); i++) {
String element = list.get(i);
System.out.println(element);
}
```
输出:
```
a
b
c
```
相关问题
array list get/set:遍历数组查找equals的对象并返回index
可以使用Java中的ArrayList类来实现遍历数组查找equals的对象并返回index的功能。ArrayList类提供了get()和set()方法,可以通过索引访问和修改元素。
以下是一个示例代码:
```java
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
// 创建一个包含Person对象的ArrayList
ArrayList<Person> persons = new ArrayList<>();
persons.add(new Person("Alice", 25));
persons.add(new Person("Bob", 30));
persons.add(new Person("Charlie", 35));
persons.add(new Person("David", 40));
// 查找名字为"Bob"的Person对象,并返回其在列表中的位置
int index = findIndex(persons, new Person("Bob", 30));
System.out.println("Index of Bob: " + index);
// 将名字为"David"的Person对象修改为年龄为50
int davidIndex = findIndex(persons, new Person("David", 40));
persons.set(davidIndex, new Person("David", 50));
// 输出修改后的列表
System.out.println(persons);
}
/**
* 遍历ArrayList查找equals的对象并返回其在列表中的位置
*
* @param list ArrayList
* @param obj 要查找的对象
* @param <T> 对象类型
* @return 对象在列表中的位置,如果未找到返回-1
*/
public static <T> int findIndex(ArrayList<T> list, T obj) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals(obj)) {
return i;
}
}
return -1;
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public boolean equals(Object obj) {
if (obj instanceof Person) {
Person other = (Person) obj;
return this.name.equals(other.name) && this.age == other.age;
}
return false;
}
public String toString() {
return "Person{name=" + name + ",age=" + age + "}";
}
}
```
在上面的例子中,我们创建了一个包含Person对象的ArrayList,并使用findIndex()方法查找名字为"Bob"的Person对象的位置。由于Person类重写了equals()方法,所以可以使用ArrayList的get()方法遍历列表并比较每个元素是否与目标对象相等。如果找到了相等的对象,就返回其在列表中的位置;否则返回-1。接着,我们使用set()方法将名字为"David"的Person对象的年龄修改为50,并输出修改后的列表。
用java定义一个list数组
可以使用Java中的ArrayList类来定义一个列表数组,示例代码如下:
```java
import java.util.ArrayList;
public class Example {
public static void main(String[] args) {
// 创建一个空的列表数组
ArrayList<String> list = new ArrayList<String>();
// 向列表数组中添加元素
list.add("Java");
list.add("Python");
list.add("C++");
// 访问列表数组中的元素
System.out.println(list.get(0)); // 输出:Java
// 遍历列表数组中的所有元素
for (String item : list) {
System.out.println(item);
}
}
}
```
在上面的示例代码中,我们首先创建了一个空的列表数组 `list`,然后使用 `add` 方法向其中添加了三个元素。接着,我们使用 `get` 方法访问了列表数组中的第一个元素,并使用循环遍历了列表数组中的所有元素。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)