如何将字符串 ArrayList 转换为对象 ArrayList

How do I convert a String ArrayList to an Object ArrayList

我想将字符串 ArrayList 转换为对象 Arraylist(如果可能的话)。 原因是因为我试图 return 一个 ArrayList 但不断收到不兼容类型错误。也许有另一种方法可以解决这个问题?

这是我写的代码:

import java.util.ArrayList;                                             
import java.util.Arrays;                                                
public class Phone                                              
{                                               
private  String Brand;                                              
private  String Type;                                               
private  String Model;                                              
private  String UniqueID;                                               
private int ManufCost;                                              
public static ArrayList<Phone> Phones = new ArrayList<Phone>();                                             
public static int counter;                                              
                                            
public Phone(String Brand, String Type, String Model, String UniqueID, int ManufCost)                                               
{                                               
 this.Brand=Brand;                                              
 this.Type=Type;                                                
 this.Model=Model;                                              
 this.UniqueID=UniqueID;                                                
 this.ManufCost=ManufCost;                                              
Phones.add(this);                                               
 counter =counter +1;                                                                                       
}       
public static ArrayList<String> getCurrentRun()                                             
{                                               
ArrayList<String> phonetext = new ArrayList<>();                                                
String temptext;                                                
                                            
for (int count = 0; count < counter+1; count++)                                             
{                                               
temptext = (Phones.get(count).Brand + " " + Phones.get(count).Type + " " + 
Phones.get(count).Model + " " + Phones.get(count).UniqueID + " " + 
Phones.get(count).ManufCost);                                               
phonetext.add(temptext);                                                
}                                               
return phonetext;                                               
}

我指的方法是getCurrentRun()

getCurrentRun 在 main 中被调用,使用:

 ArrayList<Phone> phones = Phone.getCurrentRun();                                               
for (Phone phone : phones)                                              
{                                               
  System.out.println(phone);                                                
}

这是我无法改变的。请帮我解决我的问题。
最终结果应该是打印出存储在 arraylist Phones 中的对象。

您在这里 return 从 getCurrentRun() 获取 ArrayList<String> 并期望它是 ArrayList<Phone>main 方法中。哪一个不正确,这些应该是兼容的。

可能有 2 种可能的解决方案(根据您的需要):

a) return ArrayList<Phone> 类型(或者 returning List<Phone> 是正确的方式)

public static List<Phone> getCurrentRun(){           
    return Phones; // although correct variable name should be phone as per java coding standards                                               
}

如果您想根据定义的字符串打印 Phone 详细信息,请另外覆盖 Phone class 中的 toString 方法,如下所示:

@Override
public String toString() {
    return this.Brand + " " + this.Type + " " + this.Model + " " + this.UniqueID + " " + this.ManufCost;  
}

b) 将其保持在 List<String> 中,如下所示:

List<String> phones = Phone.getCurrentRun(); // correct variable name should be phone                                               
for (String phone : phones)                                              
{                                               
  System.out.println(phone);                                                
}

另外,您还可以将此 for 循环替换为 for-each 循环。

你应该做两件事:1. 将你的 getCurrentRun() 方法修改为 return 正确的类型 - List<Phone> 和 2. 实现一个 toString() 方法来生成一个Phone.

的字符串表示

getCurrentRun() 方法修正为 return 正确的类型。请注意,以下内容复制了 phones 而不是传递原件。调用者可能会修改列表,我们不希望这影响我们的内部列表。

public static List<Phone> getCurrentRun() {
  List<Phone> copy = new ArrayList<>();
  Collections.copy(copy, phones);
  return copy;
}

实施toString()方法。请注意,这会覆盖 Object 基础 class 提供的实现(Override 注释是可选的,但很好的做法)。 JRE 将使用它从对象的实例生成一个字符串,其中语法暗示它是合适的。例如,当 main 方法遍历 List<Phone> 并将每个方法传递给 System.out.println 时,JRE 将自动调用它以生成字符串表示形式。

@Override public String toString() {
  StringJoiner joiner = new StringJoiner(" ");
  joiner.add(this.Brand);
  joiner.add(this.Type);
  joiner.add(this.Model);
  joiner.add(this.UniqueID);
  joiner.add(this.ManufCost);
  return joiner.toString();
}