【问题标题】:Java, getter for array values (array dynamically defined)Java,数组值的getter(动态定义的数组)
【发布时间】:2012-09-07 17:19:04
【问题描述】:

我有一系列 String[] 数组,它们是单词列表。类似的东西:

String[] ListOne = new String[100];
String[] ListTwo = new String[100];
/*And so on with other lists */

ListOne[0] = "word00";
ListOne[1] = "word01";
/*And so on till*/
ListLast[99] = "word 99 from last list";

现在我想为每个列表创建一个函数,给定一个数字,返回相应的元素(单词):

public String GetFromListOne(int key) { return ListOne[key];} 

有没有办法避免手动编写每个 getter 函数?

例如,在 PHP 中,我会使用魔术方法 __call
或作为带有列表名称的参数传递并动态引用它。

有没有办法在 Java 中做类似的事情?
或者是实现相同结果的替代策略?

【问题讨论】:

  • 为什么你的代码不能只做ListXXX[idx]?更简单。更小。更快。
  • 为什么不使用列表[list_id][element_id]?
  • @jmendeth OP 正在寻求一种方法来避免创建 getListOneItemgetListTwoItemgetListThreeItem 等等......
  • 此外,我认为您没有表达正确的概念。你不是说有一个Entry 对象列表,每个对象都有一个onetwothree 属性吗?
  • @LuiggiMendoza 我在问 OP 为什么他需要这些功能 :) 他的代码可以直接执行 ListOne[index]Lists[1][index] 而不是 GetFromListOne(index)。更简单。更小。 ...是的,你已经知道了。

标签: java arrays object inheritance


【解决方案1】:

你应该研究继承。

你基本上必须做的是定义一个接口(或扩展一个 List 类)

 public interface ListTest{
//**Gets keys from lists*//
GetFromListOne(int key);
}

然后

public class Listone implements ListTest{

/** methods **//
GetFromListOne(int key);
/** methods **//
}

玩得开心扩展

http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html

【讨论】:

  • 简单且运行良好。我使用抽象类而不是接口,但这就是我编写代码的方式。谢谢
【解决方案2】:

您可以使用二维数组或数组列表,并让您的函数采用 2 个参数。一个用于您想要的数组,另一个用于数组中的元素。

二维数组:

String[][] ListN = new String[100,100];

String getFromList(int n, int key) {
    return ListN[n][key];
}

或数组列表:

List<String[]> listOfArrays = new ArrayList<String[]>();
listOfArrays.add(new String[100]);
listOfArrays.add(new String[100]);


String getFromList(int n, int key) {
    return listOfArrays.get(n)[key];
}

【讨论】:

  • 这是什么魔法:) +1
  • @tuğrulbüyükışık if ("sorcery".equals("code")) this.equals("sorcery")
  • 唯一的问题是如果 OP 使用不同性质的数组,比如MyClass[] myClassArray
【解决方案3】:

你能不能有一个函数把键和列表号作为输入:

public String GetFromListOne(int list, int key) {
    switch(list):
       case 1:
           return ListOne[key];
           break;
       case 2:
           return ListTwo[key];
           break;
       ...
}

甚至更好地创建一个数组数组:

String[][] ListOfLists = new String[10];
ListOfLists[0] = new String[100];
...

public String GetFromList(int list, int key) {
    return ListOfLists[list][key];
}

否则我不知道像 __call 这样需要重写的函数

【讨论】:

    【解决方案4】:
       String[] ListFour=new String[100];
       String[] ListTwentyThree=new String[100];
       String[] ListNine=new String[100];
       String[] ListOne=new String[100];
    
       Hashtable<Integer,String[]> yourlist=new Hashtable<Integer,String[]>();      
       yourlist.put(4, ListFour);
       yourlist.put(23, ListTwentyThree);
       yourlist.put(9, ListNine);
       yourlist.put(1, ListOne);
       System.out.println(yourlist.get(4)[5]);//fifth string in ListFour
       System.out.println(yourlist.get(23)[51]);//fifty first string in List23
        System.out.println(yourlist.get(9)[1]);//first stringin ListNine
    

    另一个版本:

       Hashtable<Object,String[]> yourlist=new Hashtable<Object,String[]>();        
       yourlist.put("two multiplied by two", ListFour);
       yourlist.put(23, ListTwentyThree);
       yourlist.put(0.03, ListNine);
       yourlist.put(true, ListOne);
       System.out.println(yourlist.get("two multiplied by two")[5]);//fifth string in ListFour
       System.out.println(yourlist.get(23)[51]);//fifty first string in List23
        System.out.println(yourlist.get(true)[1]);//first stringin ListNine
    

    【讨论】:

      【解决方案5】:

      基于__call PHP 方法,你可以通过实现一个接收列表和索引的方法来实现这一点,使用泛型你可以得到类似的东西。

      public class Utility {
          public <T> T getElementFromArray(T[] array, int index) {
              if (index >= array.length || index < 0) return null;
              return array[index];
          }
      }
      

      这种方法的缺陷是不能用于原始数组持有者,如int[]。这些情况的解决方案是使用原始类型的包装类。

      public static void main(String[] args) {
          Utility u = new Utility();
          String[] ss = new String[2];
          ss[0] = "Hello";
          ss[1] = "world!";
          System.out.println(u.getElementFromArray(ss, 0));
          System.out.println(u.getElementFromArray(ss, 1));
          int[] ii = new int[2];
          ii[0] = 5;
          System.out.println(u.getElementFromArray(ii, 0)); //compile error
          //Solution: use wrapper classes
          Integer[] ii2 = new Integer[2];
          ii2[0] = 5;
          System.out.println(u.getElementFromArray(ii2, 0));
      }
      

      【讨论】:

        【解决方案6】:

        试试这个代码

        List<String[]> lists = new ArrayList<String[]>();
        
        public String getFromLists(int key) {
          List<String> res = new ArrayList<String>();
          for (String[] s: lists){
              res.add(s[key]);
          }    
          return res.get(key);
        }
        

        或更好

        public String getFromLists(int key) {
          return lists.get(key)[key];
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-03-31
          • 2017-05-11
          • 2018-09-03
          • 2015-08-27
          • 2012-08-14
          • 2013-03-31
          • 2021-12-18
          相关资源
          最近更新 更多