您好我正在尝试从链表中确定年份范围,我需要帮助。为了实现这一点,我在我的容器类中有两个名为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;
}
}
class Country:我的类Country存储国家/地区的名称,并为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);
}
}
答案 0 :(得分:1)
由于您的分配中存在约束,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
。
答案 1 :(得分:0)
在addSubscriptionYear
方法的某个地方,你需要这样的东西。
if (year < minYear) {
minYear = year;
}
if (year > maxYear) {
maxYear = year;
}
这样,如果您发现的订阅的年份早于您已找到的最早订阅,则会调整minYear
;如果您发现的订阅年晚于您已找到的最新订阅,则会调整maxYear
。
你的程序中也有一些错误;但我敢肯定,如果你继续努力,你将设法找到并删除它们。