Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/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 - Fatal编程技术网

Java列表错误(超出范围)

Java列表错误(超出范围),java,Java,我正在尝试制作一个无限世界2d游戏 该类的来源如下: package com.blazingkin.world; import java.util.LinkedList; import java.util.List; import com.blazingkin.atrox.AtroxAdventrum; public class World { public World(AtroxAdventrum aa){ chunks = new LinkedList<L

我正在尝试制作一个无限世界2d游戏

该类的来源如下:

package com.blazingkin.world;
import java.util.LinkedList;
import java.util.List;

import com.blazingkin.atrox.AtroxAdventrum;

public class World {

    public World(AtroxAdventrum aa){
        chunks = new LinkedList<List<Chunk>>();
    }

    public void setBlock(int x, int y, int newBlock){

    }

    public void setMetadata(int x, int y, int newMeta){

    }
    public int getBlock(int x, int y){
        try{
            Chunk c = chunks.get(x/64 - x%64).get(y/64 - y%64);
            return c.chunk[x%64][y%64];
        }
        catch(Exception e){
            try{
            Chunk c = new Chunk(y, x/64 - x%64, y/64 - y%64);
            chunks.get(x/64 - x%64).add(y/64 - y%64 - 1, c);
            return c.chunk[x%64][y%64]; 
        }catch(Exception e1){
            Chunk c = new Chunk(y, x/64 - x%64, y/64 - y%64);
            List<Chunk> newlist = new LinkedList<Chunk>();
            System.out.println(""+x+", "+y);
            for (int i = 0; i<y/64 - y%64; i++){
                newlist.add(i, new Chunk(y - (y/64 - y%64 - i), x/64 - x%64, y/64 - y%64 - (y/64 - y%64 - i)));
            }
            newlist.add(y/64 - y%64 - 1, c);
            for (int i = 0; i<x/64 - x%64; i++){
                newlist.add(i, new Chunk(x - (x/64 - x%64 - i), x/64 - x%64, x/64 - x%64 - (x/64 - x%64 - i)));
            }
            chunks.add(x/64 - 1 - x%64, null);

            return 0;
        }
        }




    }
    public int getMetadata(int x, int y){
        return 1;
    }



    public List<List<Chunk>> chunks;

}
package com.blazingkin.world;
导入java.util.LinkedList;
导入java.util.List;
导入com.blazingkin.atrox.AtroxAdventrum;
公共阶级世界{
公共世界(阿托品属aa){
chunks=newlinkedlist();
}
公共无效收进块(整数x、整数y、整数新块){
}
公共void setMetadata(int x、int y、int newMeta){
}
公共整数getBlock(整数x,整数y){
试一试{
Chunk c=chunks.get(x/64-x%64).get(y/64-y%64);
返回c.chunk[x%64][y%64];
}
捕获(例外e){
试一试{
块c=新块(y,x/64-x%64,y/64-y%64);
get(x/64-x%64).add(y/64-y%64-1,c);
返回c.chunk[x%64][y%64];
}捕获(异常e1){
块c=新块(y,x/64-x%64,y/64-y%64);
List newlist=newlinkedlist();
System.out.println(“+x+”,“+y”);

对于(int i=0;i我建议更换

chunks = new LinkedList<List<Chunk>>();
chunks=newlinkedlist();

chunks=newarraylist(范围);

其中RANGE可能是64,但我没有仔细阅读代码,您应该自己弄清楚。

索引:23大小:0说明了这一点。您正在尝试访问空列表的元素23。

这是一个设计问题。您正在尝试将一个元素添加到“chunks”中的一个位置,该位置是从方法参数“chunks”派生的是一个类级属性。你为什么要在给定位置添加元素?你不能只做
chunks.add(element)
。此外,我不确定您为什么要将null添加到列表中。是否改为删除?

对于您尝试执行的操作,LinkedList不是一个好的选择。首先,尝试通过索引访问LinkedList的元素是错误的(执行O(n)而不是O(1))

使用:

Map chunks=newhashmap();
私有void setChunk(整数x、整数y、块c){
if(chunks.get(x)==null){
put(x,newhashmap());
}
获取(x),放置(y,c);
}
私有块getChunk(整数x,整数y){
if(chunks.get(x)==null)
返回null;
返回chunk.get(x).get(y);
}

在您的代码中,您可以使用setChunk(1238238,-12938123,new Chunk(…),它将在x和y坐标下为Chunk设置键。要检索值,请使用getChunk(1238238,-12938123)。如果给定的x,y不存在块,则将返回null。

您不了解异常的哪一部分?大小为0。为什么您的列表为0?如果您解决了这个问题,那么您将解决您的问题。为什么我的列表为0,为什么我不能向列表的任何索引添加值?这不应该是列表的优点吗?不,您不能向列表添加值“空”的任何索引列表。您需要在每个索引处使用值初始化列表。然后,如果需要,您可以替换该值。但是,您只能按顺序添加值以最初填充列表。如果列表大小固定,则可以使用数组。然后,您可以向数组的任何索引添加值,而无需先填充整个数组。有些人认为我把你的问题记下来了,但我认为这是一个好问题。大家,要积极!问题在于你对数据结构的选择(LinkedList of List)。你想使用不同的方法(我在下面的回答中描述了一种这样的方法)。祝你好运。这无法解决该异常。这不起作用,因为我正在尝试创建一个从任意点开始的无限世界,因此我无法使用Range。你无法创建一个依赖列表的无限世界。你只能引用Integer.MAX_值块。我之所以添加null,是因为我试图确定问题是在chunk实例中,如果我添加chunksure,我会得到相同的错误。尝试添加没有位置的“chunk”,这样它将作为最后一个元素添加。另外,newlist是什么?问题是我需要能够创建一个无限的世界(溢出)从任何时候开始point@blazingkin不管你的动机如何,问题就如我所说的。答案很好,不过我会修改为使用BigInteger而不是Integer。blazingkin想要一个不受Integer.MAX_值约束的无限世界。
chunks = new ArrayList<List<Chunk>>(RANGE);
Map<Integer,Map<Integer,Chunk>> chunks = new HashMap<Integer,Map<Integer,Chunk>>();

private void setChunk(Integer x, Integer y, Chunk c) {
    if(chunks.get(x) == null) {
        chunks.put(x, new HashMap<Integer, Chunk>());
    }
    chunks.get(x).put(y, c);
}
private Chunk getChunk(Integer x, Integer y) {
    if(chunks.get(x) == null)
        return null;
    return chunks.get(x).get(y);
}