I would like to convert a String ArrayList to an Object Arraylist(if that's even possible). The reason is because I'm trying to return an ArrayList but keep getting an incompatibility type error. Maybe there's another way to solve this?
Here is the code I wrote:
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;
}
The method I am referring to is getCurrentRun()
getCurrentRun is called in main using:
ArrayList<Phone> phones = Phone.getCurrentRun();
for (Phone phone : phones)
{
System.out.println(phone);
}
And this is something I can NOT change. Please help me fix my problem.
The end result should be that it prints out the objects stored within the arraylist Phones.
CodePudding user response:
You are here returning ArrayList<String>
from getCurrentRun()
and expecting it to be ArrayList<Phone>
inside main
method. Which is not correct, these should be compatible.
There could be 2 possible solution (as per your need):
a) Either return ArrayList<Phone>
type (or returning List<Phone>
would be correct way)
public static List<Phone> getCurrentRun(){
return Phones; // although correct variable name should be phone as per java coding standards
}
And if you want to print the Phone details as per defined string, additionally override toString
method in your Phone class as below:
@Override
public String toString() {
return this.Brand " " this.Type " " this.Model " " this.UniqueID " " this.ManufCost;
}
b) Hold it in List<String>
as below:
List<String> phones = Phone.getCurrentRun(); // correct variable name should be phone
for (String phone : phones)
{
System.out.println(phone);
}
Additional thing here you can replace this for-loop with for-each
loop also.
CodePudding user response:
You should do two things: 1. fix your getCurrentRun()
method to return the correct type - List<Phone>
and 2. implement a toString()
method to produce a String representation of the Phone.
Fix the getCurrentRun()
method to return the correct type. Note that the following makes a copy of the phones
rather than passing the original. The caller might modify the List and we don't want that to affect our internal List.
public static List<Phone> getCurrentRun() {
List<Phone> copy = new ArrayList<>();
Collections.copy(copy, phones);
return copy;
}
Implement a toString()
method. Note that this overrides the implementation provided by the Object
base class (the Override annotation is optional but good practice). The JRE will use this to generate a String from an instance of the object where the syntax implies it is appropriate. For instance, when the main
method iterates over the List<Phone>
and passes each to the System.out.println
, the JRE will invoke this automatically to produce a String representation.
@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();
}