最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How to solve a problem with generalized types more correctly - Stack Overflow

programmeradmin1浏览0评论

My program has 5 classes:

1 - Abstract class Animal

public abstract class Animal {
}

2, 3 - Classes that extend an abstract class

public class Cat extends Animal implements Comparable<Cat> {
//some logic
}
public class Dog extends Animal implements Comparable<Dog> {
//some logic
}

4 - A class that contains a method for sorting animals. The sorting method must be implemented on the basis of generics. This class must necessarily use a generalized type (this is a strict condition).

public class AnimalOperations {
    public static <T extends Animal & Comparable<T>> void Sort (ArrayList<T> array){
    //sort logic
    }
}

5 - class Main. my problem is related to this class. In this class, I have to fill out an ArrayList that contains cats or dogs. Next, this array must be passed to the sorting method of the AnimalOperations class.

I know that I can solve this problem by creating two different arrays.

ArrayList<Cat> = new ArrayList<>();
ArrayList<Dog> = new ArrayList<>();

But I want to see if I can solve this problem by creating only one ArrayList() in the Main class. I tried this:

ArrayList<Animal> animal = new ArrayList<>(List.of(new Cat(), new Cat(), new Cat()));

But then when passing it to the sort method of the AnimalOperations class, I see an error "Required type: ArrayList T Provided: ArrayList Animal".

I also tried to create an ArrayList based on a generic in the Main class

public static <T extends Animal & Comparable<T>> void main(String[] args) {
    ArrayList<T> animal = new ArrayList<>(List.of(new Cat()));
}

But with this initialization, I see an error: "Required type: ArrayList T Provided: ArrayList Cat ".

My program has 5 classes:

1 - Abstract class Animal

public abstract class Animal {
}

2, 3 - Classes that extend an abstract class

public class Cat extends Animal implements Comparable<Cat> {
//some logic
}
public class Dog extends Animal implements Comparable<Dog> {
//some logic
}

4 - A class that contains a method for sorting animals. The sorting method must be implemented on the basis of generics. This class must necessarily use a generalized type (this is a strict condition).

public class AnimalOperations {
    public static <T extends Animal & Comparable<T>> void Sort (ArrayList<T> array){
    //sort logic
    }
}

5 - class Main. my problem is related to this class. In this class, I have to fill out an ArrayList that contains cats or dogs. Next, this array must be passed to the sorting method of the AnimalOperations class.

I know that I can solve this problem by creating two different arrays.

ArrayList<Cat> = new ArrayList<>();
ArrayList<Dog> = new ArrayList<>();

But I want to see if I can solve this problem by creating only one ArrayList() in the Main class. I tried this:

ArrayList<Animal> animal = new ArrayList<>(List.of(new Cat(), new Cat(), new Cat()));

But then when passing it to the sort method of the AnimalOperations class, I see an error "Required type: ArrayList T Provided: ArrayList Animal".

I also tried to create an ArrayList based on a generic in the Main class

public static <T extends Animal & Comparable<T>> void main(String[] args) {
    ArrayList<T> animal = new ArrayList<>(List.of(new Cat()));
}

But with this initialization, I see an error: "Required type: ArrayList T Provided: ArrayList Cat ".

Share Improve this question asked Nov 26, 2024 at 6:18 SergSerg 131 silver badge2 bronze badges 4
  • Something either cat, or dog, It can't be both (with exception of this). – talex Commented Nov 26, 2024 at 6:25
  • You haven't declared Animal to be comparable so ArrayList<Animal> can't be used with that Sort regardless of what it actually contains. The generic main doesn't work because the code must work for any value of T (such as Dog). – greg-449 Commented Nov 26, 2024 at 8:20
  • 1 Similar issues to your previous question, and still not clear why you are declaring generic main. – DuncG Commented Nov 26, 2024 at 8:48
  • Yes, sorry, in a real project, the Animal class implements the Comparable interface. Therefore, this assumption is incorrect, I do not have an error due to the Comparable interface. – Serg Commented Nov 26, 2024 at 9:14
Add a comment  | 

1 Answer 1

Reset to default 0

Thank you for your question!

In my opinion this becomes way more easier when you try to use your sort method without generics like this:

public static void sort(ArrayList<Animal> array) {
// Some logic
}

(Also I saw you are having a upper case character in the beginning of your method name. You should use "sort" and not "Sort".)

If you still get an error, it is always possible to cast the class to the specific one like this:

public static void sort(ArrayList<Animal> array) {
  for (Animal animal : array) {
    if (animal instanceof Cat) {
      Cat cat = (Cat) animal;
      // Do stuff with cat
    } else if (animal instanceof Dog) {
      Dog dog = (Dog) animal;
      // Do stuff with dog
    }
  }
}

I hope this helps!

发布评论

评论列表(0)

  1. 暂无评论