gpt4 book ai didi

Java-如何从列表中打印特定数组?

转载 作者:行者123 更新时间:2023-12-01 09:46:24 25 4
gpt4 key购买 nike

我创建了一个数组列表,在使用扫描仪输入名称后,我想搜索该名称是否等于 getName,然后使用 voce.get(i).toString() 打印整个数组。

与搜索 robert 一样,它会搜索所有数组列表,当找到与 robert print al array 相等的 getName 时。

抱歉我的英语不好

public class Item {
private String nome,indirizzo,cellulare;

public Item(String nome, String indirizzo, String cellulare){
this.nome = nome;
this.indirizzo = indirizzo;
this.cellulare = cellulare;
}

public String toString(){
return this.getNome() + this.getIndirizzo() + this.getCellulare();
}

public String getNome() {
if(!this.nome.isEmpty()){
return this.nome;
}
else{
return "Sconosciuto";
}
}

public void setNome(String nome) {
this.nome = nome;
}

public String getIndirizzo() {
if(!this.indirizzo.isEmpty()){
return this.indirizzo;
}
else {
return "Sconosciuto";
}
}

public void setIndirizzo(String indirizzo) {
this.indirizzo = indirizzo;
}

public String getCellulare() {
if(!this.cellulare.isEmpty()){
return this.cellulare;
}
else {
return "Sconosciuto";
}
}

public void setCellulare(String cellulare) {
this.cellulare = cellulare;
}
}

主要:

import java.util.*;



public class AggPersone {
public static void main(String[] args) {


ArrayList<Item> voce = new ArrayList<Item>();

voce.add(new Item("Robert", "Via qualcosa", "123"));
voce.add(new Item("Roberto","Via qualcosina", "123"));

Scanner input = new Scanner(System.in);
System.out.println("chi cerchi?");
String chiave = input.nextLine();


for(int i = 0; i < voce.size(); i++){
if(chiave.equals(getNome){ <---- doesn't work, how to ispect getNome?
System.out.println(voce.get(i).toString());
}
}

}
}

最佳答案

如果我理解正确,我认为您正在尝试查看来自 Scanner 的输入是否在数组列表“voce”中找到。

您需要迭代“voce”,直到看到“chiave”。

for(Item item: voce) {
if(item.getNome().equals(chiave) {
System.out.println("Found: " + item.getNome());
}
}

关于Java-如何从列表中打印特定数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37980939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com