There is such a construction

String[] massiv = { getResources().getString(R.info),"4432"}; ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(massiv.length); HashMap<String, Object> map; for (int i = 0; i < massiv.length; i++) { map = new HashMap<String, Object>(); map.put("key", massiv[i]); data.add(map); } 

Everything works, but I don’t understand how to add an element to massiv programmatically, theoretically, from other classes, so that it massiv through a cycle. Well, for example, you click on the button, the event picks up some string, and throws it into massiv and the loop goes through it. With add, you can only refer to ArrayList, but on the other hand there is a massiv.length condition. I hope you understand what I mean?

    2 answers 2

    Use the List instead of an array. It is much easier to change dynamically.

     List<String> massiv = new ArrayList<>(); massiv.add(getResources().getString(R.info)); massiv.add("4432"); ArrayList<Map<String, Object>> data = new ArrayList<>(massiv.size()); HashMap<String, Object> map; for(int i = 0; i < massiv.size(); i++) { map = new HashMap<>(); map.put("key", massiv.get(i)); data.add(map); } 

      The size of the array in Java is set when it is created and it cannot be further changed.

      In this line:

       String[] massiv = { getResources().getString(R.info),"4432"}; 

      You create an array and initialize it. As a result, you get an array with two elements. The third element cannot be added to the same array.

      You can, for example, replace any element of the array with some other, but you cannot add an element to the same array.

      However, you can create a larger array, put the elements of the old array and the new element into it, however, if you need this functionality, it is better to use appropriate ready-made solutions, such as an extensible array ( ArrayList ), or, for example, a linked list ( LinkedList ) .