英文:
Trying to solve - String index out of range
问题
以下是翻译好的代码部分:
public static Map<Integer, Celula> descompilarMapaData(Mapa mapa, String dData) {
Map<Integer, Celula> celulas = new TreeMap<Integer, Celula>();
for (int f = 0; f < dData.length(); f += 10) {
String CellData = dData.substring(f, f + 10);
List<Byte> celulaInfo = new ArrayList<Byte>();
for (int i = 0; i < CellData.length(); i++)
celulaInfo.add((byte) getNumeroPorValorHash(CellData.charAt(i)));
int caminhavel = (celulaInfo.get(2) & 56) >> 3; // 0 = not, 1 = middle, 4 = yes
boolean linhaDeVisao = (celulaInfo.get(0) & 1) != 0;
int layerObject2 = ((celulaInfo.get(0) & 2) << 12) + ((celulaInfo.get(7) & 1) << 12) + (celulaInfo.get(8) << 6)
+ celulaInfo.get(9);
boolean layerObjeto2Interac = ((celulaInfo.get(7) & 2) >> 1) != 0;
int objeto = (layerObjeto2Interac ? layerObject2 : -1);
celulas.put(f / 10, new Celula(mapa, f / 10, caminhavel != 0, linhaDeVisao, objeto));
}
return celulas;
}
请注意,翻译后的代码保留了原始的变量和注释内容,以保持代码的功能和结构不变。
英文:
I'm getting this error:
Carregando os mapas: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String
index out of range: 4800
at java.lang.String.substring(Unknown Source)
at estaticos.Criptografador.descompilarMapaData(Criptografador.java:147)
at variables.Mapa.<init>(Mapa.java:1306)
at estaticos.GestorSQL.CARREGAR_MAPAS(GestorSQL.java:696)
at estaticos.MundoDofus.criarServer(MundoDofus.java:1398)
at estaticos.VrauEMU.main(VrauEMU.java:130)
I'm trying to solve but i can't.
My code: (the original emulator is spanish, so celula = celda before)
public static Map<Integer, Celula> descompilarMapaData(Mapa mapa, String dData) {
Map<Integer, Celula> celulas = new TreeMap<Integer, Celula>();
for (int f = 0; f < dData.length(); f += 10) {
String CellData = dData.substring(f, f + 10);
List<Byte> celulaInfo = new ArrayList<Byte>();
for (int i = 0; i < CellData.length(); i++)
celulaInfo.add((byte) getNumeroPorValorHash(CellData.charAt(i)));
int caminhavel = (celulaInfo.get(2) & 56) >> 3;// 0 = nao, 1 = meio, 4 = sim
boolean linhaDeVisao = (celulaInfo.get(0) & 1) != 0;
int layerObject2 = ( (celulaInfo.get(0) & 2) << 12) + ( (celulaInfo.get(7) & 1) << 12) + (celulaInfo.get(8) << 6)
+ celulaInfo.get(9);
boolean layerObjeto2Interac = ( (celulaInfo.get(7) & 2) >> 1) != 0;
int objeto = (layerObjeto2Interac ? layerObject2 : -1);
celulas.put(f / 10, new Celula(mapa, f / 10, caminhavel != 0, linhaDeVisao, objeto));
}
return celulas;
答案1
得分: 1
问题出在迭代的边界:
for (int f = 0; f < dData.length(); f += 10) {
String CellData = dData.substring(f, f + 10);
除非 dData.length()
恰好是 10 的倍数,否则这将超出边界迭代。为了说明,下面的片段将尝试读取 0-10 和 10-20,尽管虚构的数组边界仅为 0-15。
for (let i = 0; i < 25; i += 10)
console.log(i, i + 10);
也许将上界简单地限制为 substring.length()
就足够了:
for (int f = 0; f < dData.length(); f += 10) {
int upperBound = min(f + 10, dData.length());
String CellData = dData.substring(f, upperBound));
英文:
The issue is with the iteration bounds:
for (int f = 0; f < dData.length(); f += 10) {
String CellData = dData.substring(f, f + 10);
Unless dData.length()
happens to be divisible by 10, this will iterate out of bounds. To illustrate, the below snippet will try to read 0-10 and 10-20 even though the imaginary array bounds are only 0-15.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
for (let i = 0; i < 25; i += 10)
console.log(i, i + 10);
<!-- end snippet -->
Perhaps simply capping the upper bound to substring.length()
is sufficient:
for (int f = 0; f < dData.length(); f += 10) {
int upperBound = min(f + 10, dData.length());
String CellData = dData.substring(f, upperBound));
答案2
得分: 0
我感谢你的回答
我放置了这段代码:
for (int f = 0; f < dData.length(); f += 10) {
int upperBound = Math.min(f + 10, dData.length());
String CellData = dData.substring(f, upperBound);
}
现在我得到了这个结果:
异常线程"main"java.lang.IndexOutOfBoundsException:索引:2,大小:0
在java.util.ArrayList.rangeCheck(未知来源)中
在java.util.ArrayList.get(未知来源)中
在estaticos.Criptografador.descompilarMapaData(Criptografador.java:152)中
在variables.Mapa.<init>(Mapa.java:1306)中
在estaticos.GestorSQL.CARREGAR_MAPAS(GestorSQL.java:696)中
在estaticos.MundoDofus.criarServer(MundoDofus.java:1398)中
在estaticos.VrauEMU.main(VrauEMU.java:130)中
我认为已经接近了解决方法
英文:
Thank you for the answer
I put the code:
for (int f = 0; f < dData.length(); f += 10) {
int upperBound = min(f + 10, dData.length());
String CellData = dData.substring(f, upperBound));
And i got this now:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at estaticos.Criptografador.descompilarMapaData(Criptografador.java:152)
at variables.Mapa.<init>(Mapa.java:1306)
at estaticos.GestorSQL.CARREGAR_MAPAS(GestorSQL.java:696)
at estaticos.MundoDofus.criarServer(MundoDofus.java:1398)
at estaticos.VrauEMU.main(VrauEMU.java:130)
I think it's almost there
答案3
得分: 0
public static Map<Integer, Celula> descompilarMapaData(Mapa mapa, String dData) {
Map<Integer, Celula> celulas = new TreeMap<Integer, Celula>();
//changes are here
for (int f = 0; f <= dData.length() - 10; f += 10) {
String CellData = dData.substring(f, f + 10);
List<Byte> celulaInfo = new ArrayList<Byte>();
for (int i = 0; i < CellData.length(); i++)
celulaInfo.add((byte) getNumeroPorValorHash(CellData.charAt(i)));
int caminhavel = (celulaInfo.get(2) & 56) >> 3; // 0 = no, 1 = meio, 4 = yes
boolean linhaDeVisao = (celulaInfo.get(0) & 1) != 0;
int layerObject2 = ((celulaInfo.get(0) & 2) << 12) + ((celulaInfo.get(7) & 1) << 12) + (celulaInfo.get(8) << 6)
+ celulaInfo.get(9);
boolean layerObjeto2Interac = ((celulaInfo.get(7) & 2) >> 1) != 0;
int objeto = (layerObjeto2Interac ? layerObject2 : -1);
celulas.put(f / 10, new Celula(mapa, f / 10, caminhavel != 0, linhaDeVisao, objeto));
}
return celulas;
}
英文:
public static Map<Integer, Celula> descompilarMapaData(Mapa mapa, String dData) {
Map<Integer, Celula> celulas = new TreeMap<Integer, Celula>();
//changes are here
for (int f = 0; f <= dData.length() - 10; f += 10) {
String CellData = dData.substring(f, f + 10);
List<Byte> celulaInfo = new ArrayList<Byte>();
for (int i = 0; i < CellData.length(); i++)
celulaInfo.add((byte) getNumeroPorValorHash(CellData.charAt(i)));
int caminhavel = (celulaInfo.get(2) & 56) >> 3;// 0 = nao, 1 = meio, 4 = sim
boolean linhaDeVisao = (celulaInfo.get(0) & 1) != 0;
int layerObject2 = ((celulaInfo.get(0) & 2) << 12) + ((celulaInfo.get(7) & 1) << 12) + (celulaInfo.get(8) << 6)
+ celulaInfo.get(9);
boolean layerObjeto2Interac = ((celulaInfo.get(7) & 2) >> 1) != 0;
int objeto = (layerObjeto2Interac ? layerObject2 : -1);
celulas.put(f / 10, new Celula(mapa, f / 10, caminhavel != 0, linhaDeVisao, objeto));
}
return celulas;
}
Try this I think it would work for you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论