Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.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_Arrays - Fatal编程技术网

用Java制作对象数组

用Java制作对象数组,java,arrays,Java,Arrays,我正在创建一个platformer,我有一个叫做Platform的类,它采用x位置、y位置、宽度、高度和颜色,然后将它们设置在平台内部 platform = new Platform(100, 250, 100, 10, Color.BLUE); 像这样 我可以这样调用变量: g.fillRect(platform.x, platform.y, platform.width, platform.height); 当我想获得平台的颜色时,我会这样做: g.setColor(platform.c

我正在创建一个platformer,我有一个叫做Platform的类,它采用x位置、y位置、宽度、高度和颜色,然后将它们设置在平台内部

platform = new Platform(100, 250, 100, 10, Color.BLUE);
像这样

我可以这样调用变量:

g.fillRect(platform.x, platform.y, platform.width, platform.height);
当我想获得平台的颜色时,我会这样做:

g.setColor(platform.color);
我也有一个球员的工作方式非常相似

我有一个碰撞方法:

int LXOff = (platform.x - player.width); // boundaries
int LYOff = (platform.y - player.height); // boundaries
int RXOff = (platform.x + platform.width); // boundaries
int RYOff = (platform.y + platform.height); // boundaries

int LXOff2 = (platform2.x - player.width);
int LYOff2 = (platform2.y - player.height);
int RXOff2 = (platform2.x + platform2.width);
int RYOff2 = (platform2.y + platform2.height);
if(x <= 5 || y <= 29) // numbers are specific to the perfect border of the screen
{
    return true;
}
if(x >= 495 - player.width || y >= 495 - player.height) // numbers are specific to the perfect border of the screen
{
    return true;
}
if(x >= LXOff && y >= LYOff && x <= RXOff && y <= RYOff)
{
    return true;
}
if(x >= LXOff2 && y >= LYOff2 && x <= RXOff2 && y <= RYOff2)
{
    return true;
} 
return false;
intlxoff=(platform.x-player.width);//边界
int LYOff=(platform.y-player.height);//边界
int RXOff=(platform.x+platform.width);//边界
int RYOff=(platform.y+platform.height);//边界
intlxoff2=(platform2.x-player.width);
int LYOff2=(platform2.y-player.height);
int RXOff2=(platform2.x+platform2.width);
int RYOff2=(平台2.y+平台2.height);
if(x=495-player.height)//数字特定于屏幕的完美边框
{
返回true;
}

如果(x>=LXOff&&y>=LYOff&&x=LYOff2&&x我相信对于您将使用的平台,您将创建一个新的平台对象。创建一个映射以创建平台名称作为键和平台对象作为值的映射。需要时按平台名称检索平台对象

Platform android = new Platform(....);
Platform windows = new Platform(....);
Map<String, Platform> map = new HashMap<String, Platform>();
map.put("android", android);
map.put("windows", windows);

//for retrieval
map.get("android");
platformandroid=新平台(…);
平台窗口=新平台(…);
Map Map=newhashmap();
map.put(“安卓”,安卓);
地图放置(“窗口”,窗口);
//检索
map.get(“安卓”);
如果边界是可变的,我建议为每个平台创建一个具有公共逻辑的抽象类和一个特定于平台的类

Platform[] platforms = new Platform[N]
其中N是数组的大小

如果您在创建数组时不知道数组的大小,那么最好使用

List<Platform>  platforms = new ArrayList<Platform>() 
List platforms=new ArrayList()

如果我理解你的问题,你有几个平台要画,但你也想画一些边界?O-O方法是为两者定义一个公共接口

一个选项是使用
getX()、getWidth()、getColor()
等定义一个接口,可能是RectangleLike,然后在一个大的矩形数组上循环(注意:最好使用列表)并调用相应的代码


或者,我最好定义一个接口,可能是CanDrawIntoGraphics,使用一种方法,
drawYourself(Graphics g)
。循环调用
drawYourself()。我得到了这个解决方案,您必须发送一个平台的数组列表

List <Platform> platformList =new ArrayList<Platform> ();
platformList.add(platform1);
platformList.add(platform2);
List platformList=newarraylist();
平台列表。添加(平台1);
platformList.add(platform2);
将此平台列表发送到您的方法

foreach(Platform platform:platformList){
    int LXOff = (platform.x - player.width);
    int LYOff = (platform.y - player.height);
    int RXOff = (platform.x + platform.width);
    int RYOff = (platform.y + platform.height);

    if(x >= LXOff && y >= LYOff && x <= RXOff && y <= RYOff)
    {
        return true;
    } 
}
foreach(平台:平台列表){
intlxoff=(platform.x-player.width);
int LYOff=(platform.y-player.height);
int RXOff=(platform.x+platform.width);
int RYOff=(平台y+平台高度);

如果(x>=LXOff&&y>=LYOff&&x如Java文档所述

集合(有时称为容器)只是将多个元素组合成一个单元的对象。集合用于存储、检索、操作和传递聚合数据。通常,它们表示形成一个自然组的数据项,例如扑克牌(卡片集合)、邮件文件夹(信件的集合)或电话簿(姓名到电话号码的映射)

因此,为了保持平台的数量以备将来使用,您可以创建平台集合而不是数组,因为集合具有动态扩展和收缩的能力

在编码端使用或使用平台,如果要删除duplicay,请使用设置接口

假设你有两个平台,那么你可以使用这个

List<Platform> platformList=Arrays.asList(platform1,platform2);
List platformList=Arrays.asList(platform1,platform2);
要遍历任何集合实现,请使用迭代器接口


对于Map,您可以看到由@dc.sashwat

发布的示例。您的问题到底是什么?您不知道如何创建对象数组还是其他什么?我正在尝试创建一个平台数组,这样我就可以创建多个平台,而不会使代码变得冗长。是什么阻止了您?我不知道如何确切地做到这一点。…你不知道如何用Java创建数组?你不知道语法?真的吗?