【问题标题】:Trouble determining the range of years in a linked list无法确定链表中的年份范围
【发布时间】:2014-11-16 23:55:30
【问题描述】:

大家好,我在实现这个特定问题时遇到了麻烦。每次我向列表添加订阅时,检查是否需要更新“minYear”和“maxYear”。稍后使用“minYear”和“maxYear”检查请求的订阅期是否有效。基本上我正在尝试确定订阅链接列表的年份范围。

Class SubscriptionYear:首先读取特定国家的年份和蜂窝数据。这是从文件中读取的。它需要年份和该年份的统计数据。

下面是我的 SubscriptionYear 代码:

public class SubscriptionYear {

private int year;
private double subscriptions;
SubscriptionYear next;

//stores the year and it's statistical data.
public SubscriptionYear(int year,double subscriptions)
{
    setYear(year);
    setSubscription(subscriptions);
    this.next = null;
}
//sets the year
public void setYear(int Year)
{
    this.year= Year;
}
//sets the cellular data.
public void setSubscription(double value)
{
    this.subscriptions = value;
}
public int getYear()
{
    return year;
}   
 //returns the stat data
public double getSubscription()
{
    return subscriptions;
}
public String toString()
{
    return "Number of Subscriptions: "+subscriptions;
}
//sets the node
public void setNode(SubscriptionYear next)
{
    this.next = next;
}
public SubscriptionYear getNext()
{
    return this.next;
}
}

类国家: 这读取国家名称并充当对象节点 SubscripitpnYear 的容器,用于存储年份和统计数据。它有变量字段 minYear 和 maxYear 来检查订阅何时添加到列表中是否需要更新。使用 minYear 和 maxYear 检查请求的订阅是否有效。这是我在将 SusbcriptionYear 添加到列表时遇到困难的地方 我如何检查是否需要更新 minYear 和 maxYear?并使用 minYear 和 maxYear 来检查订阅是否有效?

我的班级国家:

public class Country  {

//variable fields.
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.
public void addSubscriptionYear(int year, double subscription)
{
    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);
    }
}

//need help implementing this function
public void update(int minYear, int maxYear)
{

}

//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;
}
//overrides the equals method and returns country name if found
public boolean equals(Object obj)
{
    return this.countryNames.equalsIgnoreCase(((Country) obj).getName());
}
public boolean isEmpty()
{
    return (subscriptions == null);
}

【问题讨论】:

  • 欢迎来到 Stack Overflow!您可能希望将您发布的内容提炼成更集中的内容。想象一下,你正在给你的同事写一封电子邮件。我们很难阅读段落和段落并弄清楚您想要什么。给出几句话的背景,解释你需要什么,然后明确地陈述你的问题。这样,我们就能更轻松地为您提供帮助。
  • 还有 LALA... 当人们努力回答你的问题时,你应该选择一个“正确”的答案。哎呀,有一天你可能会试图回答一些帖子。
  • @BrianT。抱歉,我是堆栈溢出的新手。会确保这样做
  • @AlexK 感谢您的建议,因为我已经修改了问题并且更加明确。但是这个问题将如何解决。我最初解决这个问题的想法或概念是一种类似的数组越界方法。但是由于我们正在处理列表,所以它完全不同。我的想法是否正确
  • @BrianT。为了解决这个问题,我解决这个问题的最初想法或概念是一种类似的数组越界方法。但是由于我们正在处理列表,所以它完全不同。我的想法是否正确

标签: java singly-linked-list


【解决方案1】:

-1- 嗯,这里有很多代码,但我会尝试。

你的getNext() 方法让我很困惑,可能还有很多有经验的Java 开发人员。在 java 集合(例如 List)中,您可以获得一个迭代器(https://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html)。 “hasNext()”是一个布尔值“还有另一个对象吗?),可以在你的while()控件中使用。next()都返回下一个项目推进迭代器指向以下项目。因此...在您的 while() 和以下正文中都有 getNext(),看起来很可疑。

您的代码使用setNode() 设置“下一个”,并使用getNext() 检索它。按照惯例(因此对许多人来说更容易理解意图)是遵循命名约定。对于成员变量“node”,这将是setNode() & getNode()

-2- 好的,也就是说,它看起来像这样的代码:

    SubscriptionYear current = subscriptions;
    while(current.getNext()!=null)
    {
        current = current.getNext();
    }
    current.setNode(newNode);

遍历订阅并将newNode放在最后。

嗯,我放弃困惑。您使用相同的名称“订阅”作为两个截然不同的事物。在SubscriptionYear 中,它似乎是您拥有多少订阅的计数(为什么doubleint 似乎更好)。但是,在Country 中,它是您自制的SubscriptionYear 列表,拥有第一个。

IOM,解决方案的途径包括澄清您的代码,使用更好的名称,以便您了解它的作用。

-3- 除了update() 中的评论之外,您还有具体问题吗?也许您应该指出该方法应该做什么,提出逻辑等。

-- 其他观察:

-1- Country.equals() 是粗略的。如果传入的对象不是同一个类,它应该返回false,而不是ClassCastException。更好:

public boolean equals(Object obj)
{
    if (this==obj){return true;}
    if (! (obj instanceof Country)) { return false; }
    return this.countryNames.equalsIgnoreCase(((Country) obj).getName());
}

-2- 你犯了 java-developer-sin;实现equals()hashCode() 之一。重要的是 - 总是 - 实现两者,或者都不实现。更多信息在这里(阅读 equals() 和 hashCode() 描述)。

https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html

【讨论】:

  • SusbcriptionYear 有 2 个参数。一个是年份,第二个是那一年的统计数据,这就是我有一个 int 和 double 的原因。订阅是我在列表中的第一个对象。我使用 setter 和 getter 来设置节点并获取订阅年的当前节点。我从未使用过迭代器,因为我是 Java 新手。我的问题是是否要向我需要更新 minYear 和 maxYear 的列表添加订阅。我用它来确定请求的订阅是否有效。
  • 是否有一个例子或者也可以展示一个例子来说明我将如何在一个类中使用迭代器。我现在上网看了一下,有点乱。他们有一个内部类实现。如果我要在 Country 类中实现,我将如何使用迭代器。
  • IMO,您最重要的问题是使用令人困惑的术语。如果您的第二个参数具有statistical data 的含义,那为什么叫它subscriptions? ...代码不打算写一次就再也看不到了。 [我会在代码审查中失败您当前的使用。] ...花一些时间修复您的代码,重命名一些有意义的东西。 ......至于“有没有例子......”。也许你的课本和课堂笔记是个好地方?
猜你喜欢
  • 2015-01-13
  • 2014-12-02
  • 2020-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多