java arraylist 个数,java - ArrayList的IndexOutOfBounds(使用多个数字测试) - 堆栈内存溢出...

所以每当我尝试在这个特定的文本中使用这个ArrayList时,我都会遇到这个错误。 所以这是问题代码:

public static String lines(int start, int end, InputStream is) {

try {

BufferedReader fileBR = new BufferedReader(new InputStreamReader(is));

List lines = new ArrayList();

String strLine;

StringBuilder tempSB = new StringBuilder();

while ((strLine = fileBR.readLine()) != null) {

lines.add(strLine);

}

for (int i = start - 1; i < end; i++) {

tempSB.append(lines.get(i));

tempSB.append("\n");

}

line = tempSB.toString();

} catch (IOException e) {

e.printStackTrace();

}

return line;

}

我不确定代码的效率如何,但是在我的程序中获得一些基本功能后,我会继续研究效率。 问题是当我使用它时:

// Edit lines according to where the headerFile starts

public static String getHeaderArt() {

return lines(1, 21, headerFile);

}

// Change to get the introduction text from a file

public static String getIntroText() {

return lines(2, 17, storyFile);

}

public static String getStart() {

return lines(20, 27, storyFile);

}

由于某种原因,前两个代码起作用,但第三个代码不起作用。 我的故事情节文件有28行,所以我不确定它为什么不起作用。

堆栈跟踪:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 19, Size: 0

at java.util.ArrayList.rangeCheck(Unknown Source)

at java.util.ArrayList.get(Unknown Source)

at com.hathorsrpg.DefaultText.lines(DefaultText.java:42)

at com.hathorsrpg.DefaultText.getStart(DefaultText.java:86)

at com.hathorsrpg.Main.intro(Main.java:23)

at com.hathorsrpg.Main.main(Main.java:87)

这是一个没有文本文件的SSCCE:

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

public class SSCCE {

public static InputStream storyFile = SSCCE.class.getResourceAsStream("/resources/storytext.txt");

public static InputStream headerFile = SSCCE.class.getResourceAsStream("/resources/castle.txt");

public static String line;

public static String lines(int start, int end, InputStream is) {

try {

BufferedReader fileBR = new BufferedReader(new InputStreamReader(is));

List lines = new ArrayList();

String strLine;

StringBuilder tempSB = new StringBuilder();

while ((strLine = fileBR.readLine()) != null) {

lines.add(strLine);

}

for (int i = start - 1; i < end; i++) {

tempSB.append(lines.get(i));

tempSB.append("\n");

}

line = tempSB.toString();

} catch (IOException e) {

e.printStackTrace();

}

return line;

}

// Edit lines according to where the headerFile starts

public static String getHeaderArt() {

return lines(1, 21, headerFile);

}

// Change to get the introduction text from a file

public static String getIntroText() {

return lines(2, 17, storyFile);

}

public static String getStart() {

return lines(20, 27, storyFile);

}

}

感谢未来的帮助,我似乎无法弄清楚问题。 如果你想要一些东西放在一个文本文件中,这里有一些东西:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值