Java ArrayList how to add elements at the beginning

后端 未结 14 1540
忘了有多久
忘了有多久 2020-12-02 06:49

I need to add elements to an ArrayList queue whatever, but when I call the function to add an element, I want it to add the element at the beginning of the arra

14条回答
  •  春和景丽
    2020-12-02 07:24

    import java.util.*:
    public class Logic {
      List list = new ArrayList();
      public static void main(String...args) {
      Scanner input = new Scanner(System.in);
        Logic obj = new Logic();
          for (int i=0;i<=20;i++) {
            String string = input.nextLine();
            obj.myLogic(string);
            obj.printList();
          }
     }
     public void myLogic(String strObj) {
       if (this.list.size()>=10) {
          this.list.remove(this.list.size()-1);
       } else {
         list.add(strObj); 
       }
     }
     public void printList() {
     System.out.print(this.list);
     }
    }
    

提交回复
热议问题