2

I am trying to search an array of any data type (Int, Strings, Chars, etc...) to see if there exist an element that matches the one you input. You should return the index of the matching element. There are two classes being used.

The error I get is:

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

Its suggestion is to make the method static, however, doing that gives me an error in the Search class:

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

Search Class:

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;
  }
}

Runner Class:

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);


}
}

What is the best solution in this case?

Thanks,

6
  • Yah, but I wrote everything above myself. Just unsure how to deal with implementing the parts above. Commented Sep 21, 2013 at 6:45
  • @chrylis. Doesn't matter, as OP has shown his attempt. Commented Sep 21, 2013 at 6:46
  • @RohitJain If this weren't homework, I'd just say to use contains(). ;-) Commented Sep 21, 2013 at 6:48
  • @chrylis. Of course. But there is not contains() method for arrays. But I see what you are talking about. Commented Sep 21, 2013 at 6:49
  • 1
    Arrays.asList( a ).contains( e ) Commented Sep 21, 2013 at 7:22

2 Answers 2

1

You need to create an instance of your class to invoke the instance methods. Something like this:

class Demo {
    public void show() { }
}

new Demo().show();

Now, I leave it to you to instantiate your generic class.

Also, your find() method is broken. If an element is not found, it will return an index = 0. Which is a valid index in array. You should rather initialize the index to -1:

int index = -1;

Regarding your attempt to make the method static, it will give you error, because the type parameters are not applicable for static members of a class.

From Java Generics FAQs - Angelika Langer:

The scope of a class's type parameter is the entire definition of the class, except any static members or static initializers of the class. This means that the type parameters cannot be used in the declaration of static fields or methods or in static nested types or static initializers.

Sign up to request clarification or add additional context in comments.

Comments

0
  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
        
        }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.