简体   繁体   English

将用户输入的字符串与Java中具有相同Name属性的2D数组的对象匹配

[英]Matching a user input String to an object of a 2D array with the same Name attribute in Java

So I've created a 2d Array of objects of Type "Champion" that represent avatars on a 2D 10x10 grid, where null spaces indicate "open" spots and not null spaces are obviously occupied with Champions. 因此,我创建了一个2D类型的“对象”对象数组,这些对象代表2D 10x10网格上的化身,其中零位表示“开放”点,而Champions显然没有零位。

private static void createCharacter() 
{       
    System.out.println("New Character Name");
    String name = sc.next();
    System.out.println("Character x coord");
    int xc = sc.nextInt();
    System.out.println("Character y coord");
    int yc =sc.nextInt();   
    Champion tempchamp = new Champion(name,xc,yc);
    if(grid[xc][yc]!= null)
    {
        System.out.println("Error, Space Already Occuped");
    }//End If 
    else
    {
        grid[xc][yc] = tempchamp;
    }//End Else

My character constructor allows for name, x and y coordinates but the coordinates are also represented on the grid, obviously. 我的角色构造函数允许使用名称,x和y坐标,但是显然,坐标也表示在网格上。 The output looking something like this Output My issue now is finding a way to allow user to select the Character they want to move. 类似于“ Output My”问题的输出现在正在寻找一种方法,允许用户选择他们要移动的角色。 Is there a way of iterating through a 2D array to compare if any index's name attribute contains the same value as that of a user input String or would I be looking at an entirely different solution. 有没有一种方法可以遍历2D数组以进行比较,以比较任何索引的name属性包含的值与用户输入String的值相同,还是我要寻找一种完全不同的解决方案? Any and all help much appreciated. 任何和所有帮助非常感谢。 Also apologies if formatting is off. 如果格式化不正确,也表示歉意。 First post. 第一篇文章。

Currently Move method targets actual coordinates of Champions on Grid 当前Move方法的目标是网格上Champion的实际坐标

private static void moveCharacter() 
    {
        System.out.println("Enter Co-ords of Champ to Move");
        int posx = sc.nextInt();
        int posy = sc.nextInt();
        if(grid[posx][posy]!=null)
        {
            String moving = grid[posx][posy].getName();
            System.out.println("Enter Target Coordinates");
            int tarx = sc.nextInt();
            int tary = sc.nextInt();
            if(grid[tarx][tary]==null)
            {
                grid[posx][posy]=null;
                Champion champ = new Champion(moving, tarx, tary);
                grid[tarx][tary]=champ;
                printgrid();
                double distance = calculateDistance(posx, posy, tarx, tary);
                System.out.println("Distace Travelled " + distance);
            }//End if
        }//End If Pos Null
        else
            System.out.println("Target Position is Empty");
            {
            moveCharacter();
            }//End Else
    }//End Move Character

Was toying around with something like 在玩类似的东西

String ChampToMove = "Brian";
for(int i = 0; i<grid.length; i++)
    {
        for(int j = 0; j<grid[i].length;j++)
        {
            if(grid[i][j].getName.equals(ChampToMove))
            {
             moveCharacter(ChampToMove );
            }
        }//End Nested For

The last piece isn't currently what I'm using but more what I was playing around with earlier, its just a general gist of what I was thinking would work, however I feel it's not efficient whatsoever. 最后一部分不是我目前正在使用的东西,而是我之前正在玩的东西,它只是我一直认为可以起作用的一般要点,但是我觉得它效率不高。

I would recommend using a binarySearch, it has a runtime of O(nlog(n)), compared to your current algorithm, which has a runtime of O(n^2). 与您当前的算法(运行时间为O(n ^ 2))相比,我建议使用binarySearch,它的运行时间为O(nlog(n))。

A possible way to implement is : 一种可能的实现方式是:

for(int i = 0; i<grid.length; i++)
    {
            if(binarySearch(grid[i], ChampToMove))
            {
             moveCharacter(ChampToMove );
             break;
            }
        }//End Nested For

and the binarySearch algorithm : 和binarySearch算法:

public static boolean binarySearch(String[] a, String x) {
int low = 0;
int high = a.length - 1;
int mid;

while (low <= high) {
    mid = (low + high) / 2;
    System.out.println(a[mid]);
    if (a[mid].compareTo(x) < 0) {
        low = mid + 1;
    } else if (a[mid].compareTo(x) > 0) {
        high = mid - 1;
    } else {
        return true;
    }
}

return false;
}

if you have any questions about how any of this works, just ask. 如果您对此有任何疑问,请问。

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

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