如何在Java中高效地从另一个函数引用对象/值。

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

How do I reference an object/value from another function efficiently in Java

问题

我是一个年轻的程序员正在为一个游戏开发 Java 插件我遇到了一个问题即无法从另一个事件的变量中获取 XYZ 坐标并将其传递到当前事件将该变量设为公共变量会导致代码出现严重问题而将所有值放入数组中虽然可行但感觉不太合适我觉得在不必要的地方消耗了太多精力

   **相关代码**
```java
ArrayList<Location> GenLocations = new ArrayList<Location>();

public void firstEvent(){
    for(int i = 0; i < GenLocations.size(); i++) {
        Location L = GenLocations.get(i);
        if(event.getClickedBlock().getLocation().equals(L)) {
            // 非相关代码...
        }
    }
}

public void secondEvent(){
    if(searchInventory(event.getWhoClicked().getInventory(), "太长的名字", 8, true)) {
        /* 我希望从 GenLocations.get(i) 获取正确的 XYZ 坐标。
        没有 if(event.getClickedBlock().getLocation().equals(L)){},
        很难确定 GenLocations.get(i) 是否是正确的位置,
        也不想通过大量的 if 语句来比较数组数据和当前事件,
        这样很繁琐而且可能效率低下。 */
    }
}

是否有一种方法可以在不使用大量循环的情况下实现这一点,或者我现在的做法是正确的呢?


<details>
<summary>英文:</summary>

I am a young programmer, and I am working on a java plugin for a game and I ran into a problem where I can&#39;t get an XYZ coordinate from another event&#39;s variable, to my current event. Making the variable public would cause major problems for my code and putting all the values in an array works, but it doesn&#39;t feel right, and I think I am using to much energy where it isn&#39;t needed. 

   **My Relevant Code:**

ArrayList<Location> GenLocations = new ArrayList<Location>();

public void firstEvent(){
for(int i = 0; i < GenLocations.size(); i++) {
Location L = GenLocations.get(i);
if(event.getClickedBlock().getLocation().equals(L)) {
//Non-relevant code...
}
}
}

public void secondEvent(){
if(searchInventory(event.getWhoClicked().getInventory(),"Too big name", 8, true)) {
/* Here is where I want the correct XYZ coord from GenLocations.get(i). It is hard to tell
whether GenLocations.get(i) is the right position without
if(event.getClickedBlock().getLocation().equals(L)){} or going through a bunch of if statements
comparing the data of arrays with the current event which is
pain staking long and possibly inefficient /*

			}

}



Is there a way to do this without using tons of for loops or am I doing it the way I should be?


</details>


# 答案1
**得分**: 0

我假设这两种方法在同一个类中,不确定您是要存储所有位置还是只存储一个位置。如果您想要存储一个变量,您可以在类中创建一个成员变量,比如`Location firstEventLocation`,当您在`firstEvent()`中找到位置时,将其存储在局部变量中,如`firstEventLocation = GenLocations.get(index)`。然后在`secondEvent`中,您可以从变量`firstEventLocation`引用第一个位置,只要确保在有可能为null的情况下检查`firstEventLocation`是否为null。还有一个小注意事项,尽量遵循Java命名约定,变量名应以小写字母开头,`GenLocations`在命名约定下应为`genLocations`。

<details>
<summary>英文:</summary>

I am assuming the 2 methods are in the same class, not sure if you are looking to store all the locations or just one. If you are trying to store one variable you  can create a member variable in the class say `Location firstEventLocation` and when you find in Location in `firstEvent()` store it in the local variable `firstEventLocation = GenLocations.get(index)`. The then `secondEvent` you can reference the first location from the variable `firstEventLocation`, just be sure to check that firstEventLocation is not null if there is a chance it could be. One other small note, try to stick to java naming conventions variable names for should start lowercase letters, `GenLocations` under naming conventions should be `genLocations` 

</details>



# 答案2
**得分**: 0

如果问题是定位包含位置数据的数组的索引,那么引入另一个数据结构来跟踪索引。

例如,一个HashMap,以对象为键,包含数组内索引作为其值,将有助于高效地找到您的项。

<details>
<summary>英文:</summary>

If the problem is locating the index of the array that contains the location data, then introduce another data structure to track the index.

For instance, a HashMap, keyed on the object and containing the index within the array as its value item, would help you efficiently find your item.

</details>



huangapple
  • 本文由 发表于 2020年7月25日 11:32:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63084039.html
匿名

发表评论

匿名网友

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

确定