Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/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 ArrayList索引错误_Java_Arrays - Fatal编程技术网

Java ArrayList索引错误

Java ArrayList索引错误,java,arrays,Java,Arrays,我正试图完成一个项目,该项目使用图形用户界面搜索美国的间歇泉数据库。我的一个数组列表不断出现错误。我会将我的数据包含在程序中,但长度为10000个条目。非常感谢您的帮助。这是在BlueJ中完成的 错误:java.lang.IndexOutOfBoundsException:索引:0,大小:0 数据库代码: import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.*; import j

我正试图完成一个项目,该项目使用图形用户界面搜索美国的间歇泉数据库。我的一个数组列表不断出现错误。我会将我的数据包含在程序中,但长度为10000个条目。非常感谢您的帮助。这是在BlueJ中完成的

错误:java.lang.IndexOutOfBoundsException:索引:0,大小:0

数据库代码:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class GeyserDatabase
{
private ArrayList < Geyser > Geysers;
private ArrayList < Eruption > Eruptions;

public GeyserDatabase() {

    Geysers = new ArrayList < Geyser >();
    Eruptions = new ArrayList < Eruption >();
}

public void readGeyserData(String filename){

    try{ 

        File f = new File(filename);
        Scanner sc = new Scanner(f);
        String text;

        // keep reading as long as there is more data
        while(sc.hasNext()) {
            text = sc.nextLine();

            Eruption e = new Eruption(text);
            Eruptions.add(e);

        }
        sc.close();
    }
    catch(IOException e) {
        System.out.println("Failed to read the data file: " + filename);
    }
    createGeyserList();
}

public void addEruption (Eruption e){
    Eruptions.add(e);
}

public ArrayList <Eruption> getEruptionList(){

    return Eruptions;

}

public ArrayList <Geyser> getGeyserList(){

    return Geysers;
}

public int getNumEruptions() {
    return Eruptions.size();
}

public int getNumEruptions(int m,int d,int y) {
    int count = 0;
    for ( Eruption e : Eruptions) {
        if (e.getMonth()==m && e.getDay()==d && e.getYear()==y) {
            count++;
        }
    }
    return count;
}

public Eruption getLateNightEruption() {
    Eruption latestEruption = Eruptions.get(0);
    int latestHour   = latestEruption.getHour();
    int latestMinute = latestEruption.getMinute();
    for ( Eruption e: Eruptions ) {
        if (e.getHour() > latestHour || (e.getHour()==latestHour && e.getMinute()>latestMinute)) {
            latestEruption = e;
            latestHour = e.getHour();
            latestMinute = e.getMinute();
        }
    }
    return latestEruption;
}

public ArrayList < Eruption > getEruptions(String geyser) {
    ArrayList < Eruption > result = new ArrayList < Eruption > ();
    for ( Eruption e: Eruptions ) {
        if (e.getGeyserName().startsWith(geyser)) {
            result.add(e);
        }
    }
    return result;
}

private void createGeyserList(){
    ArrayList<String>nameList = new ArrayList<String>();
    // create temporary list of unique geyser names
    for(Eruption e:Eruptions){
        if(!nameList.contains(e.getGeyserName())){
            nameList.add(e.getGeyserName());
        }
    }

    // create a list of geysers
    ArrayList<Geyser>geyserList = new ArrayList<Geyser>();
    for(String s:nameList){
        Geyser g = new Geyser(s);

        // count number of eruptions for current geyser name
        for(Eruption e:Eruptions){
            if(e.getGeyserName().equals(g.getName()))
                g.increment();
        }
        geyserList.add(g);
    }
}

public int getMostGeysers(){

    return Geysers.size();

}

public Geyser findMostActiveGeyser(){

    Geyser MostEruptions = Geysers.get(0);
    int mostActive = MostEruptions.getNumEruptions();
    for( Geyser g : Geysers){

        if(g.getNumEruptions() > mostActive){
            MostEruptions =g;
            mostActive = g.getNumEruptions();

        }

    }
    return MostEruptions;
}

public Geyser findLeastActiveGeyser()
{

    Geyser leastActiveGeyser = Geysers.get(0);
    int leastActive = leastActiveGeyser.getNumEruptions();
    for(Geyser g: Geysers)
    {
        if(g.getNumEruptions() < leastActive)
        {
            leastActiveGeyser = g;
            leastActive = g.getNumEruptions();
        }
    }
    return leastActiveGeyser;
}

public String findDayWithMostEruptions(int y){

    int dayMost = 1;
    int monthMost = 1;
    int maxSoFar = getNumEruptions(1,1,y);
    for(int m = 1; m<=12; m++){

        for(int d = 1; d<=31;d++){
            int eruptionsDay = getNumEruptions(m,d,y);

            if(eruptionsDay>maxSoFar){
                dayMost = d;
                monthMost = m;
                maxSoFar=eruptionsDay;
            }

        }

    }
    return monthMost + "/" + dayMost + "/" + y + "Eruptions: " + maxSoFar;
}

public static void main(String args[]){
    GeyserDatabase gdb = new GeyserDatabase();

    }
   }
喷发代码:

import java.util.Scanner;
public class Eruption
{
int month;
int day;
int year;
int hour;
int minute;
String geyserName;

public Eruption(String info){

    Scanner scnr = new Scanner(info);
    scnr.useDelimiter("/|,|:");
    month = scnr.nextInt();
    day = scnr.nextInt();
    year = scnr.nextInt();


    geyserName = scnr.next();
    hour = scnr.nextInt();

    minute = scnr.nextInt();



}

public int getMonth(){

    return month;

}

public int getDay(){

    return day;

}

public int getYear(){

    return year;
}

public int getHour(){

    return hour;

}

public int getMinute(){

    return minute;
}

public String getGeyserName(){

    return geyserName;
}

public  String toString( ){

    String geyserString = geyserName + " " + "on" + month +"/"+ day +"/" + year + "at" + hour +":"+ minute;
    return geyserString;

}

public static void main(String [] args){
    Eruption e = new Eruption("1/2/2016,Old Faithful,10:46");
    if (e.getMonth() == 1) {
        System.out.println("getMonth worked well");
        }
      }


   }

我的天啊,那么多的代码。这个错误是从哪里抛出的?请显示你的堆栈跟踪,这样我们就可以知道你面对错误的地方。当你得到错误时,它会告诉你它是哪一行吗?这将非常有用:)
错误:java.lang.IndexOutOfBoundsException:Index:0,Size:0
通常意味着您正在尝试访问一个空的数据结构。你能在发生这种情况的地方添加相关的代码吗?我的天哪-那么多代码。这个错误是从哪里抛出的?请显示你的stacktrace,这样我们就可以知道你遇到错误的地方当你得到错误时,它会告诉你它是哪一行吗?这将非常有用:)
错误:java.lang.IndexOutOfBoundsException:Index:0,Size:0
通常意味着您正在尝试访问一个空的数据结构。您能在发生这种情况的地方添加相关代码吗?
 import java.util.Scanner;
public class Geyser
{
String geyserName;
int count = 0;
public Geyser (String name){

  geyserName = name;

}

public void increment (){



    count++; 

}

public String getName() {
    return geyserName;
}

public int getNumEruptions() {
    return count;
}

public static void main(String args[]) {
    Geyser g = new Geyser("xyz");
    if (g.getName().equals("xyz")) {
        System.out.println("getName worked well.");
    }
    else {
        System.out.println("getName did not work well.");
    }
    if (g.getNumEruptions() == 0) {
        System.out.println("getNumEruptions worked well.");
    }
    else {
        System.out.println("getNumEruptions did not work well.");
    }
    g.increment();
      if (g.getNumEruptions() == 1) {
        System.out.println("getNumEruptions worked well.");
    }
    else {
        System.out.println("getNumEruptions did not work well.");
     }
    }
   }
import java.util.Scanner;
public class Eruption
{
int month;
int day;
int year;
int hour;
int minute;
String geyserName;

public Eruption(String info){

    Scanner scnr = new Scanner(info);
    scnr.useDelimiter("/|,|:");
    month = scnr.nextInt();
    day = scnr.nextInt();
    year = scnr.nextInt();


    geyserName = scnr.next();
    hour = scnr.nextInt();

    minute = scnr.nextInt();



}

public int getMonth(){

    return month;

}

public int getDay(){

    return day;

}

public int getYear(){

    return year;
}

public int getHour(){

    return hour;

}

public int getMinute(){

    return minute;
}

public String getGeyserName(){

    return geyserName;
}

public  String toString( ){

    String geyserString = geyserName + " " + "on" + month +"/"+ day +"/" + year + "at" + hour +":"+ minute;
    return geyserString;

}

public static void main(String [] args){
    Eruption e = new Eruption("1/2/2016,Old Faithful,10:46");
    if (e.getMonth() == 1) {
        System.out.println("getMonth worked well");
        }
      }


   }