尝试解决 – 字符串索引超出范围

huangapple go评论108阅读模式
英文:

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 &quot;main&quot; 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.&lt;init&gt;(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&lt;Integer, Celula&gt; descompilarMapaData(Mapa mapa, String dData) {
		Map&lt;Integer, Celula&gt; celulas = new TreeMap&lt;Integer, Celula&gt;();
		for (int f = 0; f &lt; dData.length(); f += 10) {
			String CellData = dData.substring(f, f + 10);
			List&lt;Byte&gt; celulaInfo = new ArrayList&lt;Byte&gt;();
			for (int i = 0; i &lt; CellData.length(); i++)
				celulaInfo.add((byte) getNumeroPorValorHash(CellData.charAt(i)));
			int caminhavel = (celulaInfo.get(2) &amp; 56) &gt;&gt; 3;// 0 = nao, 1 = meio, 4 = sim
			boolean linhaDeVisao = (celulaInfo.get(0) &amp; 1) != 0;
			int layerObject2 = ( (celulaInfo.get(0) &amp; 2) &lt;&lt; 12) + ( (celulaInfo.get(7) &amp; 1) &lt;&lt; 12) + (celulaInfo.get(8) &lt;&lt; 6)
					+ celulaInfo.get(9);
			boolean layerObjeto2Interac = ( (celulaInfo.get(7) &amp; 2) &gt;&gt; 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 &lt; 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 &lt; 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 &lt; 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 &lt; 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 &quot;main&quot; 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.&lt;init&gt;(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&lt;Integer, Celula&gt; descompilarMapaData(Mapa mapa, String dData) {
        Map&lt;Integer, Celula&gt; celulas = new TreeMap&lt;Integer, Celula&gt;();
        //changes are here
          for (int f = 0; f &lt;= dData.length() - 10; f += 10) {
            String CellData = dData.substring(f, f + 10);
            List&lt;Byte&gt; celulaInfo = new ArrayList&lt;Byte&gt;();
            for (int i = 0; i &lt; CellData.length(); i++)
                celulaInfo.add((byte) getNumeroPorValorHash(CellData.charAt(i)));
            int caminhavel = (celulaInfo.get(2) &amp; 56) &gt;&gt; 3;// 0 = nao, 1 = meio, 4 = sim
            boolean linhaDeVisao = (celulaInfo.get(0) &amp; 1) != 0;
            int layerObject2 = ((celulaInfo.get(0) &amp; 2) &lt;&lt; 12) + ((celulaInfo.get(7) &amp; 1) &lt;&lt; 12) + (celulaInfo.get(8) &lt;&lt; 6)
                    + celulaInfo.get(9);
            boolean layerObjeto2Interac = ((celulaInfo.get(7) &amp; 2) &gt;&gt; 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.

huangapple
  • 本文由 发表于 2020年4月7日 07:09:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/61070351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定