【发布时间】:2015-01-13 22:03:21
【问题描述】:
您好,我正在尝试从链接列表中确定年份范围,我需要帮助。为了实现这一点,我的容器类中有两个名为 minYear 和 maxYear 的变量,其中包含 subscriptionYears 链表。 SubscriptionYear 读取年份和该年份的蜂窝数据。 country 类存储国家名称,是链表订阅Year 的容器,存储每个国家的年份和蜂窝数据。
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);
}
}
【问题讨论】:
-
您是否要在订阅中查找最早和最近的年份?或者尝试计算 1960 年和 2012 年有多少订阅?另外,你有没有从中得到任何编译错误?
-
我会问与@DavidWallace 相同的问题。也只是为了好奇,你为什么不使用内置的 LinkedList 类来维护年份列表?
-
@RJadhav 这是教授的一个项目,我们必须遵循她的指导方针。因为她让我们在不使用 API 的情况下编写实现
-
@DavidWallace 我们将使用 minYears 和 maxYear 变量来使用“minYear”和“maxYear”来检查请求的订阅期是否有效。
-
这是否意味着只有 minyear 和 maxyear 之间的订阅年有效?