【问题标题】:Determining the range of year in a linked list确定链表中的年份范围
【发布时间】: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 之间的订阅年有效?

标签: java singly-linked-list


【解决方案1】:

由于您的分配中有一个约束条件,即必须使用minYear=9999maxYear=0 实例化Country 对象,因此您可以更新您的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.minYearthis.maxYear

【讨论】:

  • 认真的吗?您正在教初学者为此使用三元运算符?
  • @DavidWallace 这是一件坏事吗?不完全确定问题是什么。我通常会将代码重构为更小、更短的方法。但是 FWIW,我不想再混淆 OP。
  • 大多数初学者对三元运算符感到困惑。这就是我在回答中使用简单的if 语句的原因。
  • @DavidWallace 感谢您对初学者的关注。恕我直言,与其他不太友好的老师相比,他们最好从优秀、温和的老师(比如你自己)那里学习先进的东西。有那么一刻,我以为你会拉"clarity over brevity" 卡:-)
  • @DavidWallace 我确实在回答的最后简要解释了这些三元运算符在做什么。
【解决方案2】:

在您的 addSubscriptionYear 方法中的某个地方,您需要这样的东西。

if (year < minYear) {
    minYear = year;
}

if (year > maxYear) {
    maxYear = year;
}

这样,如果您发现订阅的年份早于您已经找到的最早年份,则您正在调整 minYear;如果您发现订阅的年份比您已经找到的最新订阅年份晚,那么您正在调整 maxYear

你的程序也有一些错误;但我敢肯定,如果你继续努力,你会设法找到并删除它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-02
    • 2020-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多