【问题标题】:Array Search of Generics java泛型java的数组搜索
【发布时间】:2013-09-21 06:35:33
【问题描述】:

我正在尝试搜索任何数据类型(Int、Strings、Chars 等)的数组,以查看是否存在与您输入的元素匹配的元素。您应该返回匹配元素的索引。有两个类被使用。

我得到的错误是:

"Cannot make a static reference to the non-static method find(Object[], Object) from the type ArraySearch"

它的建议是使方法静态,但是这样做会在 Search 类中出现错误:

"Cannot make a static reference to the non-static type E".

搜索类:

public class ArraySearch<E> {
public int find (E[] array, E item) {
      int index = 0;
      for (int i = 0; i < array.length; i++) {
          if (array[i].equals(item)) {
              System.out.println("There is a element " + array[i] + 
                      " at index " + i);
              index = i;
              break;
          }
      }
      return index;
  }
}

跑步者类:

public class ArraySearchRunner {

public static void main(String[] args) {

    String[] strings = new String[]{"Jim", "Tim", "Bob", "Greg"};
    Integer[] ints = new Integer[]{1, 2, 3, 4, 5};

    ArraySearch.find(strings, "Bob");
    ArraySearch.find(ints, 4);


}
}

在这种情况下最好的解决方案是什么?

谢谢,

【问题讨论】:

  • 是的,但我把所有的东西都写在了自己之上。只是不确定如何处理上述部分的实现。
  • @chrylis。没关系,因为 OP 已经展示了他的尝试。
  • @RohitJain 如果这不是家庭作业,我只想说使用contains()。 ;-)
  • @chrylis。当然。但是数组没有contains() 方法。但我明白你在说什么。
  • Arrays.asList( a ).contains( e )

标签: java arrays search generics


【解决方案1】:

您需要创建类的实例来调用实例方法。像这样的:

class Demo {
    public void show() { }
}

new Demo().show();

现在,我让你来实例化你的泛型类。

另外,您的 find() 方法已损坏。如果没有找到一个元素,它将返回一个index = 0。这是数组中的有效索引。您应该将index 初始化为-1

int index = -1;

关于您尝试将方法设为静态,它会给您错误,因为类型参数不适用于类的static 成员。

来自Java Generics FAQs - Angelika Langer

类的类型参数的范围是类的整个定义,除了类的任何静态成员或静态初始化器。这意味着类型参数不能用于静态字段或方法的声明或静态嵌套类型或静态初始值设定项中。

【讨论】:

  • 明白了。这是有道理的。感谢您的帮助。
【解决方案2】:
  public class ArraySearch<E> 
        {
        public int find (E[] array, E item) 
        {
              int c = 0;
              for (int i = 0; i < array.length; i++) 
              {
                  if (array[i].equals(item)) 
                  {
                      c++;
                       
                  }
              }
              System.out.println("There is a element " + item + 
                      " repeated " + c + " time(s)");
              return c;
          }
        
        public static void main(String[] args) 
        {
        
            String[] strings = new String[]{"Jim", "Tim", "Bob", "Greg","Bob"};
            String[] strings2 = new String[]{"Jim", "Tim", "Bob", "Greg","Bob"};
            Integer[] ints = new Integer[]{1, 2, 3, 4, 5,2};
            Double[] dbl= new Double[] {1.2,3.6,8.4,8.4,8.4,3.6};
            ArraySearch arr = new ArraySearch();
            arr.find(strings, "Bob");
            arr.find(strings, "Tim");
            arr.find(ints, 2);
            arr.find(dbl, 8.4);
            enter code here
        
        }
        }

【讨论】:

    猜你喜欢
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 2011-06-13
    • 1970-01-01
    相关资源
    最近更新 更多