您好,我正在尝试从链表中确定年份范围,我需要帮助。为了实现这一点,我在我的容器类中有两个名为minYear和maxYear的变量,其中包含subscriptionYears链接列表。 SubscriptionYear读取年份和该年份的蜂窝数据。国家类别存储国家名称,是链接列表subscriptionYear的容器,该列表存储每个国家的年份和移动电话数据。
minYear设置为9999。maxYear设置为0。每次我向列表添加订阅时,我都会更新minYear和maxYear。我使用“ minYear”和“ maxYear”来检查请求的订阅期是否有效。我如何实现minYear和maxYear来检查请求的订阅是否有效。
我的课程Subscriptionyear将年份和该特定年份的移动数据存储为两倍。
//stores the year and statistical subscription data for that year
public class SubscriptionYear {
private int year;
private double subscriptions;
SubscriptionYear next;
public SubscriptionYear(int year,double subscriptions)
{
setYear(year);
setSubscription(subscriptions);
this.next = null;
}
public void setYear(int Year)
{
this.year= Year;
}
public void setSubscription(double value)
{
this.subscriptions = value;
}
public int getYear()
{
return year;
}
public double getSubscription()
{
return subscriptions;
}
public String toString()
{
return "Number of Subscriptions: "+subscriptions;
}
public void setNode(SubscriptionYear next)
{
this.next = next;
}
public SubscriptionYear getNext()
{
return this.next;
}
}
国家(地区)类:我的国家(地区)类存储国家的名称,并充当susbcriptionYear链接列表的容器,其中包含该国家的年份和统计信息。我能够创建列表并成功打印列表,但似乎无法解决上面的问题。统计数据始于1960年至2012年。
public class Country {
private String countryNames;
private SubscriptionYear subscriptions;
private int minYear;
private int maxYear;
public Country(String country)
{
this.countryNames = country;
this.subscriptions = null;
this.maxYear = 0;
this.minYear = 9999;
}
//adds the subscription and updates the minYear and maxYear
//Dont think i set up the minYear and maxYear correctly.
public void addSubscriptionYear(int year, double subscription)
{
SubscriptionYear newNode = new SubscriptionYear(year, subscription);
if(this.isEmpty())
{
newNode.setNode(subscriptions);
subscriptions = newNode;
if(newNode.getYear()==1960)
{
this.minYear++;
}
}
else{
SubscriptionYear current = subscriptions;
while(current.getNext()!=null)
{
if(current.getYear()==1960)
{
this.minYear++;
}
else if(current.getYear()==2012)
{
this.maxYear++
}
current = current.getNext();
}
current.setNode(newNode);
}
}
//overrides the toString method and prints out the countries.
public String toString()
{
String result="";
result += "\n"+this.countryNames;
SubscriptionYear current = subscriptions;
while(current!=null)
{
result+="\t"+current.getSubscription();
current = current.getNext();
}
return result;
}
//returns countryName
public String getName()
{
return this.countryNames;
}
public boolean isEmpty()
{
return (subscriptions == null);
}
}
最佳答案
由于分配中存在必须使用Country
和minYear=9999
实例化maxYear=0
对象的约束,因此可以更新Country.addSubscription()
方法以执行比较和验证。
所以这样的事情应该工作:
public void addSubscriptionYear(int year, double subscription) {
// check if this new node's year is within the valid range
if(year > 2012 || year < 1960)
throw new IllegalArugmentException("New node's year is not within the 1960 and 2012 range"); // or however else you want to handle this
// check if this new node has a year earlier than this.minYear
this.minYear = this.minYear > year ? year : this.minYear;
// check if this new node has a year late than this.maxYear
this.maxYear = this.maxYear < year ? year : this.maxYear;
SubscriptionYear newNode = new SubscriptionYear(year, subscription);
if(this.isEmpty()) {
newNode.setNode(subscriptions);
subscriptions = newNode;
}
else{
SubscriptionYear current = subscriptions;
while(current.getNext()!=null)
current = current.getNext();
current.setNode(newNode);
}
}
每次添加新节点时,我们都会检查新年是否小于
this.minYear
或大于this.maxYear
。如果是,我们将相应地调整this.minYear
和this.maxYear
。