Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 确定链表中的年数范围时遇到问题_Java_Singly Linked List - Fatal编程技术网

Java 确定链表中的年数范围时遇到问题

Java 确定链表中的年数范围时遇到问题,java,singly-linked-list,Java,Singly Linked List,大家好,我在解决这个问题时遇到了困难。每次我向列表添加订阅时,检查是否需要更新“minYear”和“maxYear”。稍后使用“minYear”和“maxYear”检查请求的订阅期是否有效。基本上,我试图确定订阅链接列表的年数范围 Class SubscriptionYear:首先读取特定国家的年份和蜂窝数据。这是从文件中读取的。它接收当年的数据并统计当年的数据 以下是我的SubscriptionEAR代码: public class SubscriptionYear { private in

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

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

以下是我的SubscriptionEAR代码:

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;
}
}
类别国家: 这将读取国家名称,并充当存储年份和统计数据的对象节点subscriptpnyear的容器。它具有变量字段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);
}
-一,- 嗯,这里有很多代码,但我会试试

您的
getNext()
方法让我感到困惑,可能还有很多有经验的Java开发人员。在java集合(例如List)中,您可以获得一个迭代器()。“hasNext()”是一个布尔值“还有其他对象吗?”,可以在
while()
控件中使用。
next()
都返回下一项,并且推进迭代器指向下一项。因此,
getNext()
while()中都有
,并且在下面的主体中看起来可疑

您的代码使用
setNode()
来设置“next”,并使用
getNext()
来检索它。通常(因此对许多人来说更容易理解目的)是遵循命名约定。对于成员变量“node”,这将是
setNode()
getNode()

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

    SubscriptionYear current = subscriptions;
    while(current.getNext()!=null)
    {
        current = current.getNext();
    }
    current.setNode(newNode);
遍历订阅并将
newNode
放在末尾

嗯,我很困惑地放弃了。你使用相同的名称“订阅”作为两个截然不同的东西。在
SubscriptionYear
中,它似乎是一个你拥有多少订阅的计数(为什么
double
int
似乎更好).然而,在
国家/地区
中,是您自己制作的
订阅EAR
列表,占据第一位

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

-三,- 除了
update()
中的注释之外,您是否有一个特定的问题?也许您应该指出该方法应该做什么,提出逻辑等

-- 其他意见:

-一,-
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());
}
-二,- 您已承诺java开发人员sin;实现了
equals()
hashCode()
中的一个。务必始终同时实现这两个,或者两者都不实现。这里有更多信息(请阅读equals()和hashCode()说明)


欢迎使用Stack Overflow!您可能希望将您发布的内容浓缩成更为集中的内容。想象您正在给同事写电子邮件。我们很难阅读段落和段落,并找出您想要的内容。请给出几句背景知识,解释您需要的内容,然后陈述您的想法明确提问。这样,我们就更容易帮助你了。还有拉拉……当人们努力回答你的问题时,你应该选择一个“正确答案””“回答。“见鬼,总有一天你可能也会看到一些试图回答问题的帖子。”@BrianT。很抱歉,我不熟悉堆栈溢出。我一定会做的that@AlexK谢谢你的建议,因为我已经修改了这个问题,而且更加明确。但这个问题将如何解决呢。我最初解决这个问题的想法或概念是一种类似的方法,即数组越界。但由于我们处理的是列表,它就完全不同了。我的想法正确吗track@BrianT. 为了解决这个问题,我最初的想法或概念是采用类似的方法来解决这个问题。但由于我们处理的是列表,它就完全不同了。我的思路是否正确?SusbcriptionYear采用2个参数。一个是年份,第二个是那一年的统计数据,这就是为什么我有一个int和double。订阅是列表中的第一个对象。我使用setter和getter设置节点并获取subscriptionYear的当前节点。我从未使用过迭代器,因为我是java新手。我的问题是,如果要在列表中添加订阅,我需要更新minYear和maxYear。我使用它来确定请求的订阅是否有效。是否有一个示例,或者也可以显示一个示例,说明我将如何在类中使用迭代器。我现在在网上看了看,有点困惑。它们有一个内部类实现。如果我要在类Country.IMO中实现,我将如何使用迭代器,您最重要的问题是使用令人困惑的术语。如果您的第二个参数的含义是统计数据,那么究竟为什么称它为订阅。。。代码不打算写一次就再也看不见了。[我会在代码审查中不通过您当前的使用。]。。。花点时间修改你的代码,重新命名使之有意义。。。至于“有没有一个例子……”。也许你的课本和课堂笔记是个好地方?