Java - 任意返回重复不止一个的对象
Java - returning object arbitrarily that repeats more then one
我有一个 class 依赖于我之前创建的三个 classes。
我在第 4 个 class 中有一个方法,其中要求我获取最新的 Car
对象。
如果有更多,从一个这样的,其中一个将被 return 任意编辑。
调用的方法getNewest()
发布当前的 class 和之前的供您参考以建立联系:
前 3 class -> 汽车
public class Car {
private Owner _owner; //an object from 1st class
private String _manufacturer;
private boolean _airbag;
private boolean _leasingOrRental;
private int _km, _seats, _year;
private int _price;
private int _LevyPrice;
private Bid _highestBid;//an object from 2st class
public Car(Owner ow, String mfr, boolean isAirbag, boolean leasOrRent, int kilNum, int seatNum, int date, int price, int levy, Bid high) // Constructor
{
_owner = new Owner(ow);
_manufacturer = mfr;
_airbag = isAirbag;
_leasingOrRental = leasOrRent;
_km = kilNum;
_seats = seatNum;
_year = date;
_price = price;
_LevyPrice = levy;
_highestBid = new Bid(high);
}
public Car(Car other) //copy constrcutor
{
_owner = new Owner(other._owner);
_manufacturer = other._manufacturer;
_airbag = other._airbag;
_leasingOrRental = other._leasingOrRental;;
_km = other._km;
_seats = other._seats;;
_year = other._year;
_price = other._price;
_LevyPrice = other._LevyPrice;
_highestBid = new Bid(other._highestBid);
}
public Owner getOwner()
{
return new Owner(_owner);
}
public String getManufacturer()
{
return _manufacturer;
}
public boolean getAirbag()
{
return _airbag;
}
public boolean getIsleasingOrRental()
{
return _leasingOrRental;
}
public int getKm() {
return _km;
}
public int getSeats() {
return _seats;
}
public int getYear() {
return _year;
}
public int getLevyPrice() {
return _LevyPrice;
}
public Bid getHighestBid() {
return new Bid(_highestBid);
}
public void makeBid(Bid bid)
{
if (_highestBid.getBidPrice() < bid.getBidPrice())
_highestBid.setBidPrice(bid.getBidPrice());
}
public boolean isAttractive()
{
return _airbag == true && _leasingOrRental == true && (_km > 0 && _km < 20000 ) && _year >= 3;
}
public boolean fitForFamily(int kids)
{
return _seats - 2 >= kids;
}
public boolean overUsedCar()
{
final int KILOMERTES = 12000;
return _km > KILOMERTES*(2022 - _year);
}
public String toString()
{
return "Manufacturer: "+_manufacturer+"\n"
+"Year: "+_year+"\n"
+"Owner: "+_owner.toString()+"\n"
+"Highest bid: "+_highestBid.getBidPrice();
}
}
第 4 和当前 class CarSales
和
public class CarSales {
private Car[] _cars;
int _noOfCars;
public CarSales(int size)
{
_cars = new Car[size];
_noOfCars = 0;
}
public boolean addCar(Car car)
{
int size = _cars.length+1;
//_cars = new Car[_noOfCars];
if(_cars.length < _noOfCars)
{
_cars[size] = new Car(car);
return true;
}
else
{
return false;
}
}
public Car getNewest()
{
Car newest = _cars[0];
Car [] _new = new Car[_noOfCars];
int size = 0;
for (int i = 1; i < _cars.length; i++)
if(newest.getYear() < _cars[i].getYear())
newest = _cars[i];
for (int i = 0; i < _cars.length; i++)
if (newest.getYear() == _cars[i].getYear())
size++;
for (int i = 0; i < _cars.length; i++)
if (newest.getYear() == _cars[i].getYear())
_new[i] = new Car(_cars[i]);
return _new[0];
}
}
在我创建的方法中,我正在 return 第一个对象,但如果我从一开始就这样做,我可以在第一个循环之后 return 它。
但是,如果我有多个对象,我想任意选择 return 对象。
另外,如果有办法提高效率,我将不胜感激。
不能使用 ArrayList
lang,如添加或复制或 indexOf 必须按逻辑进行。
提前感谢所有帮助者和反馈。
在 class CarSales
的构造函数中,您初始化 _cars
。这是您问题中代码的相关行。
_cars = new Car[size];
这将创建一个包含 size
个元素的数组,但每个元素都是空的。因此 _noOfCars
的目的是告诉您数组 _cars
中有多少 non-null 个元素。因此,在方法 getNewest
中的所有 for
循环中(在 class CarSales
中),您需要遍历数组 [=16= 的所有 non-null 元素] 而不是遍历所有元素。因此每个 for
循环应该是:
for (int i = 0; i < _noOfCars; i++)
因此,方法 addCar
(也在 class CarSales
中)需要更新 _noOfCars
。这是更正的方法。
public boolean addCar(Car car) {
if (_noOfCars < _cars.length) {
_cars[_noOfCars++] = car;
return true;
}
else {
return false;
}
}
我假设在方法 getNewest
中,您想 return 从包含所有最新汽车的数组中随机选择一个元素。在下面的代码中,我使用 class ThreadLocalRandom 为数组生成一个随机索引。
public Car getNewest() {
Car newest = _cars[0];
int size = 0;
for (int i = 1; i < _noOfCars; i++)
if (newest.getYear() < _cars[i].getYear())
newest = _cars[i];
for (int i = 0; i < _noOfCars; i++)
if (newest.getYear() == _cars[i].getYear())
size++;
Car[] _new = new Car[size];
int ndx = 0;
for (int i = 0; i < _noOfCars; i++)
if (newest.getYear() == _cars[i].getYear())
_new[ndx++] = new Car(_cars[i]);
return _new[ThreadLocalRandom.current().nextInt(0, _new.length)];
}
这是一个完整的例子。由于您没有 post classes Owner
和 Bid
的代码,我为它们做了最少的定义,我还添加了一个 main
方法class Car
从而得到完整的可以编译的程序 运行.
import java.util.concurrent.ThreadLocalRandom;
public class Car {
private Owner _owner; // an object from 1st class
private String _manufacturer;
private boolean _airbag;
private boolean _leasingOrRental;
private int _km, _seats, _year;
private int _price;
private int _LevyPrice;
private Bid _highestBid;// an object from 2st class
// Constructor
public Car(Owner ow,
String mfr,
boolean isAirbag,
boolean leasOrRent,
int kilNum,
int seatNum,
int date,
int price,
int levy,
Bid high) {
_owner = ow; // new Owner(ow);
_manufacturer = mfr;
_airbag = isAirbag;
_leasingOrRental = leasOrRent;
_km = kilNum;
_seats = seatNum;
_year = date;
_price = price;
_LevyPrice = levy;
_highestBid = high; // new Bid(high);
}
// copy constrcutor
public Car(Car other) {
_owner = other._owner; // new Owner(other._owner);
_manufacturer = other._manufacturer;
_airbag = other._airbag;
_leasingOrRental = other._leasingOrRental;
_km = other._km;
_seats = other._seats;
_year = other._year;
_price = other._price;
_LevyPrice = other._LevyPrice;
_highestBid = other._highestBid; // new Bid(other._highestBid);
}
public Owner getOwner() {
return _owner; // new Owner(_owner);
}
public String getManufacturer() {
return _manufacturer;
}
public boolean getAirbag() {
return _airbag;
}
public boolean getIsleasingOrRental() {
return _leasingOrRental;
}
public int getKm() {
return _km;
}
public int getSeats() {
return _seats;
}
public int getYear() {
return _year;
}
public int getLevyPrice() {
return _LevyPrice;
}
public Bid getHighestBid() {
return _highestBid; // new Bid(_highestBid);
}
public void makeBid(Bid bid) {
if (_highestBid.getBidPrice() < bid.getBidPrice())
_highestBid.setBidPrice(bid.getBidPrice());
}
public boolean isAttractive() {
return _airbag == true && _leasingOrRental == true && (_km > 0 && _km < 20000)
&& _year >= 3;
}
public boolean fitForFamily(int kids) {
return _seats - 2 >= kids;
}
public boolean overUsedCar() {
final int KILOMERTES = 12000;
return _km > KILOMERTES * (2022 - _year);
}
public String toString() {
return "Manufacturer: " + _manufacturer + "\n" + "Year: " + _year + "\n" + "Owner: "
+ _owner.toString() + "\n" + "Highest bid: " + _highestBid.getBidPrice();
}
public static void main(String[] args) {
CarSales carSales = new CarSales(5);
Car carOne = new Car(new Owner("George"), "mfr", true, true, 0, 5, 2020, 1, 1, new Bid(2));
carSales.addCar(carOne);
Car carTwo = new Car(new Owner("May"), "mfr", true, true, 0, 5, 2020, 2, 2, new Bid(4));
carSales.addCar(carTwo);
System.out.println(carSales.getNewest());
}
}
class CarSales {
private Car[] _cars;
private int _noOfCars;
public CarSales(int size) {
_cars = new Car[size];
_noOfCars = 0;
}
public boolean addCar(Car car) {
if (_noOfCars < _cars.length) {
_cars[_noOfCars++] = car;
return true;
}
else {
return false;
}
}
public Car getNewest() {
Car newest = _cars[0];
int size = 0;
for (int i = 1; i < _noOfCars; i++)
if (newest.getYear() < _cars[i].getYear())
newest = _cars[i];
for (int i = 0; i < _noOfCars; i++)
if (newest.getYear() == _cars[i].getYear())
size++;
Car[] _new = new Car[size];
int ndx = 0;
for (int i = 0; i < _noOfCars; i++)
if (newest.getYear() == _cars[i].getYear())
_new[ndx++] = new Car(_cars[i]);
return _new[ThreadLocalRandom.current().nextInt(0, _new.length)];
}
}
class Bid {
private double bidPrice;
public Bid(double price) {
bidPrice = price;
}
public double getBidPrice() {
return bidPrice;
}
public void setBidPrice(double bidPrice) {
this.bidPrice = bidPrice;
}
}
class Owner {
private String name;
public Owner(String name) {
this.name = name;
}
public String toString() {
return name;
}
}
我有一个 class 依赖于我之前创建的三个 classes。
我在第 4 个 class 中有一个方法,其中要求我获取最新的 Car
对象。
如果有更多,从一个这样的,其中一个将被 return 任意编辑。
调用的方法getNewest()
发布当前的 class 和之前的供您参考以建立联系:
前 3 class -> 汽车
public class Car {
private Owner _owner; //an object from 1st class
private String _manufacturer;
private boolean _airbag;
private boolean _leasingOrRental;
private int _km, _seats, _year;
private int _price;
private int _LevyPrice;
private Bid _highestBid;//an object from 2st class
public Car(Owner ow, String mfr, boolean isAirbag, boolean leasOrRent, int kilNum, int seatNum, int date, int price, int levy, Bid high) // Constructor
{
_owner = new Owner(ow);
_manufacturer = mfr;
_airbag = isAirbag;
_leasingOrRental = leasOrRent;
_km = kilNum;
_seats = seatNum;
_year = date;
_price = price;
_LevyPrice = levy;
_highestBid = new Bid(high);
}
public Car(Car other) //copy constrcutor
{
_owner = new Owner(other._owner);
_manufacturer = other._manufacturer;
_airbag = other._airbag;
_leasingOrRental = other._leasingOrRental;;
_km = other._km;
_seats = other._seats;;
_year = other._year;
_price = other._price;
_LevyPrice = other._LevyPrice;
_highestBid = new Bid(other._highestBid);
}
public Owner getOwner()
{
return new Owner(_owner);
}
public String getManufacturer()
{
return _manufacturer;
}
public boolean getAirbag()
{
return _airbag;
}
public boolean getIsleasingOrRental()
{
return _leasingOrRental;
}
public int getKm() {
return _km;
}
public int getSeats() {
return _seats;
}
public int getYear() {
return _year;
}
public int getLevyPrice() {
return _LevyPrice;
}
public Bid getHighestBid() {
return new Bid(_highestBid);
}
public void makeBid(Bid bid)
{
if (_highestBid.getBidPrice() < bid.getBidPrice())
_highestBid.setBidPrice(bid.getBidPrice());
}
public boolean isAttractive()
{
return _airbag == true && _leasingOrRental == true && (_km > 0 && _km < 20000 ) && _year >= 3;
}
public boolean fitForFamily(int kids)
{
return _seats - 2 >= kids;
}
public boolean overUsedCar()
{
final int KILOMERTES = 12000;
return _km > KILOMERTES*(2022 - _year);
}
public String toString()
{
return "Manufacturer: "+_manufacturer+"\n"
+"Year: "+_year+"\n"
+"Owner: "+_owner.toString()+"\n"
+"Highest bid: "+_highestBid.getBidPrice();
}
}
第 4 和当前 class CarSales
和
public class CarSales {
private Car[] _cars;
int _noOfCars;
public CarSales(int size)
{
_cars = new Car[size];
_noOfCars = 0;
}
public boolean addCar(Car car)
{
int size = _cars.length+1;
//_cars = new Car[_noOfCars];
if(_cars.length < _noOfCars)
{
_cars[size] = new Car(car);
return true;
}
else
{
return false;
}
}
public Car getNewest()
{
Car newest = _cars[0];
Car [] _new = new Car[_noOfCars];
int size = 0;
for (int i = 1; i < _cars.length; i++)
if(newest.getYear() < _cars[i].getYear())
newest = _cars[i];
for (int i = 0; i < _cars.length; i++)
if (newest.getYear() == _cars[i].getYear())
size++;
for (int i = 0; i < _cars.length; i++)
if (newest.getYear() == _cars[i].getYear())
_new[i] = new Car(_cars[i]);
return _new[0];
}
}
在我创建的方法中,我正在 return 第一个对象,但如果我从一开始就这样做,我可以在第一个循环之后 return 它。 但是,如果我有多个对象,我想任意选择 return 对象。 另外,如果有办法提高效率,我将不胜感激。
不能使用 ArrayList
lang,如添加或复制或 indexOf 必须按逻辑进行。
提前感谢所有帮助者和反馈。
在 class CarSales
的构造函数中,您初始化 _cars
。这是您问题中代码的相关行。
_cars = new Car[size];
这将创建一个包含 size
个元素的数组,但每个元素都是空的。因此 _noOfCars
的目的是告诉您数组 _cars
中有多少 non-null 个元素。因此,在方法 getNewest
中的所有 for
循环中(在 class CarSales
中),您需要遍历数组 [=16= 的所有 non-null 元素] 而不是遍历所有元素。因此每个 for
循环应该是:
for (int i = 0; i < _noOfCars; i++)
因此,方法 addCar
(也在 class CarSales
中)需要更新 _noOfCars
。这是更正的方法。
public boolean addCar(Car car) {
if (_noOfCars < _cars.length) {
_cars[_noOfCars++] = car;
return true;
}
else {
return false;
}
}
我假设在方法 getNewest
中,您想 return 从包含所有最新汽车的数组中随机选择一个元素。在下面的代码中,我使用 class ThreadLocalRandom 为数组生成一个随机索引。
public Car getNewest() {
Car newest = _cars[0];
int size = 0;
for (int i = 1; i < _noOfCars; i++)
if (newest.getYear() < _cars[i].getYear())
newest = _cars[i];
for (int i = 0; i < _noOfCars; i++)
if (newest.getYear() == _cars[i].getYear())
size++;
Car[] _new = new Car[size];
int ndx = 0;
for (int i = 0; i < _noOfCars; i++)
if (newest.getYear() == _cars[i].getYear())
_new[ndx++] = new Car(_cars[i]);
return _new[ThreadLocalRandom.current().nextInt(0, _new.length)];
}
这是一个完整的例子。由于您没有 post classes Owner
和 Bid
的代码,我为它们做了最少的定义,我还添加了一个 main
方法class Car
从而得到完整的可以编译的程序 运行.
import java.util.concurrent.ThreadLocalRandom;
public class Car {
private Owner _owner; // an object from 1st class
private String _manufacturer;
private boolean _airbag;
private boolean _leasingOrRental;
private int _km, _seats, _year;
private int _price;
private int _LevyPrice;
private Bid _highestBid;// an object from 2st class
// Constructor
public Car(Owner ow,
String mfr,
boolean isAirbag,
boolean leasOrRent,
int kilNum,
int seatNum,
int date,
int price,
int levy,
Bid high) {
_owner = ow; // new Owner(ow);
_manufacturer = mfr;
_airbag = isAirbag;
_leasingOrRental = leasOrRent;
_km = kilNum;
_seats = seatNum;
_year = date;
_price = price;
_LevyPrice = levy;
_highestBid = high; // new Bid(high);
}
// copy constrcutor
public Car(Car other) {
_owner = other._owner; // new Owner(other._owner);
_manufacturer = other._manufacturer;
_airbag = other._airbag;
_leasingOrRental = other._leasingOrRental;
_km = other._km;
_seats = other._seats;
_year = other._year;
_price = other._price;
_LevyPrice = other._LevyPrice;
_highestBid = other._highestBid; // new Bid(other._highestBid);
}
public Owner getOwner() {
return _owner; // new Owner(_owner);
}
public String getManufacturer() {
return _manufacturer;
}
public boolean getAirbag() {
return _airbag;
}
public boolean getIsleasingOrRental() {
return _leasingOrRental;
}
public int getKm() {
return _km;
}
public int getSeats() {
return _seats;
}
public int getYear() {
return _year;
}
public int getLevyPrice() {
return _LevyPrice;
}
public Bid getHighestBid() {
return _highestBid; // new Bid(_highestBid);
}
public void makeBid(Bid bid) {
if (_highestBid.getBidPrice() < bid.getBidPrice())
_highestBid.setBidPrice(bid.getBidPrice());
}
public boolean isAttractive() {
return _airbag == true && _leasingOrRental == true && (_km > 0 && _km < 20000)
&& _year >= 3;
}
public boolean fitForFamily(int kids) {
return _seats - 2 >= kids;
}
public boolean overUsedCar() {
final int KILOMERTES = 12000;
return _km > KILOMERTES * (2022 - _year);
}
public String toString() {
return "Manufacturer: " + _manufacturer + "\n" + "Year: " + _year + "\n" + "Owner: "
+ _owner.toString() + "\n" + "Highest bid: " + _highestBid.getBidPrice();
}
public static void main(String[] args) {
CarSales carSales = new CarSales(5);
Car carOne = new Car(new Owner("George"), "mfr", true, true, 0, 5, 2020, 1, 1, new Bid(2));
carSales.addCar(carOne);
Car carTwo = new Car(new Owner("May"), "mfr", true, true, 0, 5, 2020, 2, 2, new Bid(4));
carSales.addCar(carTwo);
System.out.println(carSales.getNewest());
}
}
class CarSales {
private Car[] _cars;
private int _noOfCars;
public CarSales(int size) {
_cars = new Car[size];
_noOfCars = 0;
}
public boolean addCar(Car car) {
if (_noOfCars < _cars.length) {
_cars[_noOfCars++] = car;
return true;
}
else {
return false;
}
}
public Car getNewest() {
Car newest = _cars[0];
int size = 0;
for (int i = 1; i < _noOfCars; i++)
if (newest.getYear() < _cars[i].getYear())
newest = _cars[i];
for (int i = 0; i < _noOfCars; i++)
if (newest.getYear() == _cars[i].getYear())
size++;
Car[] _new = new Car[size];
int ndx = 0;
for (int i = 0; i < _noOfCars; i++)
if (newest.getYear() == _cars[i].getYear())
_new[ndx++] = new Car(_cars[i]);
return _new[ThreadLocalRandom.current().nextInt(0, _new.length)];
}
}
class Bid {
private double bidPrice;
public Bid(double price) {
bidPrice = price;
}
public double getBidPrice() {
return bidPrice;
}
public void setBidPrice(double bidPrice) {
this.bidPrice = bidPrice;
}
}
class Owner {
private String name;
public Owner(String name) {
this.name = name;
}
public String toString() {
return name;
}
}