简体   繁体   中英

Generic parameter in method for generic arraylist

The public method addToFront is supposed to take a generic parameter (T) that is then added to the front of an empty array list. I don't know the syntax for the generic parameter and cannot find anything online for help.

public class RandomStuffContainer<T extends Comparable<T>>
{

  ArrayList<T> array = new ArrayList<T>();
    
  public  void main(String[] args){
    ArrayList<T> array = new ArrayList<T>();
  }
        
  public void addToFront()
  {
    array.add(0, T);
  } 

}

I just need help with putting in the generic parament for addToFront.

If you are trying to add an object of type T in method addToFront to your array List. The first thing you need to think of the parameter input to the method. Something like this, addToFront(T obj). And inside the method, you can array.add(0, obj); The parameter in addToFront obj type should be similar to the ArrayList.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM