从另一个方法添加arraylist中的元素 [英] Add element in arraylist from another method

查看:82
本文介绍了从另一个方法添加arraylist中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从另一种方法添加myarray中的任何元素,但是当我尝试使用相同的方法时它会工作



低于我的整个代码



我尝试过:



i cant add any element in myarray from another method , but when i try from the same method it works

below my whole code

What I have tried:

import java.util.ArrayList;


public class NewClass  {
 
 public static ArrayList<Integer> myarray = new ArrayList<>(); 
    
           public static void main(String [] args) {
           
          System.out.print(myarray);

   }
    
    public static void work (){ 
        
        myarray.add(5);
                

    }

   public static void work1 (){ 
        
        myarray.add(6);
                

    }
           

}

推荐答案

Bot方法按预期工作。

例如:

Bot methods work as expected.
For instance:
public static void main(String [] args) {
          work();
          work1();
          System.out.println(myarray);

   }

产生:

produces:

[5,6]


这篇关于从另一个方法添加arraylist中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆