简体   繁体   English

我如何从多维数组中搜索然后返回特定值?

[英]How do I search for and then return a specific value from a multi-dim array?

I'm quite a beginner on Java and have taken to creating a simple game based on what I've read, watched, listened to online. 我是Java的初学者,并且根据我在网上阅读,观看和聆听的内容来制作一个简单的游戏。 Just a simple game that'll help me cement techniques and syntax whilst going along. 只是一个简单的游戏,它将帮助我在学习过程中巩固技巧和语法。

One of the features it has is random (sort of) terrain generation. 它具有的功能之一是随机(某种)地形生成。 It's a 2d flying game and what I've done is create an int[][] which draws a line of land. 这是一个2D飞行游戏,我所做的就是创建一个int [] []来画一条地线。 It uses a for loop to cycle through the array and then a random number generator to determine whether the next 'piece' of land is a pixel higher, lower, or on the same level. 它使用for循环在数组中循环,然后使用随机数生成器确定下一个“地块”是高,低还是处于同一像素水平。

    int a = lengthOfLand;
    int b = startHeightOfLand;
    int[][] land = new int[a][500];

    land[0][b] = 1;
    int y = b;
    for(x = 1; x < a; x++){
        switch(getRandomNumber(3)){
            case 0: land[x][y - 1] = 1;
                y--;
            case 1: land[x][y] = 1;
            case 2: land[x][y + 1] = 1;
                y++;

(I'm sure there are a million better ways for random terrain generation, but I'm just getting to grips with everything and learning new techniques, so this way works for me). (我敢肯定,有100万种更好的方法可以随机生成地形,但是我将掌握一切并学习新技术,因此这种方法对我有效)。

The above is all called in my initializer class. 以上都是在我的初始化器类中调用的。 In the rendering area of the game, we take the array and say if the value = 1, draw this image at the x, y values provided. 在游戏的渲染区域中,我们采用数组并说如果值= 1,则在提供的x,y值处绘制此图像。 Obviously, all other values in the array = 0, so these aren't rendered. 显然,数组中的所有其他值= 0,因此不会呈现这些值。 So what I see is a nice line with some ups, downs, flats, all generated (semi-)randomly, and (most importantly) differently, every time I run the game. 因此,我看到的是一条很好的线条,上面包含一些起伏,起伏,单位,每次我运行游戏时,它们都是(半)随机产生的(最重要的是)是不同的。 Basically, I use this multi-dim array to plot every pixel on the screen. 基本上,我使用此多维数组来绘制屏幕上的每个像素。 The values that = 1 are rendered as the top of the land. 等于1的值将呈现为地形的顶部。 Perfect. 完善。

However, what I'd like to do is start up a separate array, to carry the land on where this one left off. 但是,我想做的是启动一个单独的数组,以将这块土地停在原地。 So I need to be able to draw from the land array, the height at which we end (this is different every time the game is run due to the random element). 因此,我需要能够从Land数组中绘制出我们结束的高度(由于随机元素,每次运行游戏时,该高度都不同)。 Basically, I need to find out the index of y when y = 1 and x = a. 基本上,当y = 1且x = a时,我需要找出y的索引。 So searching for and then returning a specific value in a certain 'column' of a multi-dim array. 因此,在多维数组的某个“列”中搜索然后返回特定值。

How can I do this? 我怎样才能做到这一点? I assume I can use a method from the Array Class, but as a beginner, I don't really have a clue where to look and what to do with it. 我想我可以使用Array类中的方法,但是作为一个初学者,我真的不知道在哪里看以及如何使用它。

Use a nested for :) like this 像这样使用嵌套的:)

for (x = 0; x < a; x++) {
        for (i = 0; i < 500; i++) {
            land[x][i];
            if (your condition here){
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM