首先,我是ArrayList的新手。我有这个程序,SalesPerson对象将遍布全球20个城市。对于每次访问,程序将要求用户输入城市名称,城市规模和邮政编码。这是使用for循环和ArrayList完成的。但是当我想要显示所有城市时,它会打印出奇怪的角色。我可以知道我哪里出错吗?
Below is the sample output.
Enter name of city: Singapore
Enter size of city: Small
Enter postal code: 132115
==
Enter name of city: Indonesia
Enter size of city: Big
Enter postal code: 111222
==
Cities Visited: [travellingapp.City@42a57993, travellingapp.City@75b84c92]
这是我的代码:
TravellingApp.java
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
SalesPerson s1 = new SalesPerson();
for(int j=1; j<=20; j++)
{
System.out.print("Enter name of city: ");
String name = sc.nextLine();
System.out.print("Enter size of city: ");
String size = sc.nextLine();
System.out.print("Enter postal code: ");
int postalCode = sc.nextInt();
sc.nextLine();
System.out.println("___________________________________________");
City c1 = new City(name, size, postalCode);
s1.addCity(c1);
}
System.out.print("Cities Visited: "+ s1.returnListOfCities());
}
SalesPerson.java(Class)
public class SalesPerson {
private ArrayList<City>listOfCities = new ArrayList<City>();
public void addCity(City c1)
{
listOfCities.add(c1);
}
public ArrayList<City> returnListOfCities()
{
return listOfCities;
}
}
City.java(Class)
public class City {
private String nameOfCity;
private String sizeOfCity;
private int postalCode;
public City(String nameOfCity, String sizeOfCity, int postalCode)
{
this.nameOfCity = nameOfCity;
this.sizeOfCity = sizeOfCity;
this.postalCode = postalCode;
}
public String getNameOfCity() {
return nameOfCity;
}
public String getSizeOfCity() {
return sizeOfCity;
}
public int getPostalCode() {
return postalCode;
}
public void setNameOfCity(String nameOfCity) {
this.nameOfCity = nameOfCity;
}
public void setSizeOfCity(String sizeOfCity) {
this.sizeOfCity = sizeOfCity;
}
public void setPostalCode(int postalCode) {
this.postalCode = postalCode;
}
}
答案 0 :(得分:2)
当您致电require 'fruity'
arr = Array.new(1000) { |seed|
# seed is used to create the same array on each script run,
# hence the same benchmark results will be produced
Random.new(seed).rand(1..10).to_s
}
class Array
def difference(other)
h = other.each_with_object(Hash.new(0)) { |e,h| h[e] += 1 }
reject { |e| h[e] > 0 && h[e] -= 1 }
end
end
compare do
_coreyward do
arr.reduce(Hash.new(0)) { |counts, value|
counts[value] += 1
counts
}.max[1] <= 2
end
_wayne do
arr.group_by(&:itself).none? do |_key, values|
values.count > 2
end
end
_sagarpandya82 do
arr.sort_by(&:to_i).each_cons(3).none? { |a,b,c| a == b && b == c }
end
_tadman do
arr.sort.slice_when { |a,b| a != b }.map(&:length).max.to_i <= 2
end
_cary do
arr.difference(arr.uniq*2).empty?
end
_akuhn do
count = Hash.new(0)
arr.none? { |each| (count[each] += 1) > 2 }
end
_oneneptune do
arr.each_with_object(Hash.new(0)) { |element,counts|
counts[element] += 1
}.values.max < 3
end
_glykyo do
arr.uniq.map{ |element| arr.count(element) }.max <= 2
end
_vlasiak do
arr.none? { |el| arr.count(el) > 2 }
end
end
时,该方法会返回s1.returnListOfCities()
个对象的arraylist。 City
无法识别此格式。
您可以使用迭代器来迭代Arraylist。
System.out.println()
答案 1 :(得分:1)
Java中的每个类都来自Object
,它有一个toString()
方法,这会返回一个类似你看到的字符串,而System.out.println()
方法会使用它,所以如果你没有'在你的课堂上实现它,它将使用Object
(多态)中的那个。
像
这样的东西public String toString(){
return "City { "+nameOfCity+", " + sizeOfCity +", "+postal code+"}";
}