behzad razzaqi
behzad razzaqi

Reputation: 1511

How can add an element to an array?

I wrote this code with a String array:

public static String[] prgmNameList = {"bbbbb", "aaaaa"};

My question is now, how can I add a new item to that array like this:

prgmNameList.add("cccc");

Upvotes: 1

Views: 109

Answers (5)

Karthik R
Karthik R

Reputation: 5786

On simpler terms, note these following points:

  • Array size is always fixed.(In your example you fixed the array size to 2 by adding 2 elements)
  • Arrays operate based on index starting from '0' zero, like - prgmNameList[0] will return 1st element added in the array
  • Array size cannot be changed at any point of time. If you need size to be variable, choose one of List implementations
  • ArrayList is the best option for your need that can define itself as an 'Array that can shrink or grow'

Sample code:

public static List<String> prgmNameList= new ArrayList<String>();
prgmNameList.add("bbb");
prgmNameList.add("bbb");
prgmNameList.add("ccc");
prgmNameList.remove("bbb"); //Removes by object resolved by equals() method
prgmNameList.remove(2); //Removes by index

Upvotes: 1

Alon
Alon

Reputation: 2929

You have to use ArrayList like that

 ArrayList<String> al = new ArrayList<>();


  // add elements to the array list
  al.add("C");
  al.add("A");
  al.add("E");
  al.add("B");

if you want to use array as you did you have to know the number of elements that you want to add

String[] myList = new String[10];

and then

myList[4]="AA"

-- this is not possible to add to myList. I explain you how ArrayList works and then you will understand.

ArrayList is an class that contains Array from objects. every time you add it check if it have place to store the data in the array if not it creates new array bigger and store the data.

So ArrayList this is the solution (or any other list) if you want to add to myList you will have to implement arratList..

Upvotes: 0

Sachin Tanna
Sachin Tanna

Reputation: 46

You have created an Array which can not grow as it's fixed in size. You need to create a list in order to add new elements as shown below.

List<String> prgmNameList = new ArrayList<String>();
prgmNameList.add("aaaa");
prgmNameList.add("bbbb");
prgmNameList.add("cccc");

Upvotes: 0

tilois
tilois

Reputation: 682

The method you are looking for is defined for a Collection, but you are using an array with an array initializer.

I suggest switching to the List:

public static List<String> prgmNameList= new ArrayList<>(Arrays.asList("bbbbb","aaaaa"))

Then you can call add on it because now it is a list. Btw.: Try to prevent having mutable variables in static variables.

Upvotes: -1

Chetan Kinger
Chetan Kinger

Reputation: 15212

prgmNameList is an array. The size of an array object cannot be changed once it has been created. If you want a variable size container, use collections. For example, use an ArrayList :

List<String> prgmNameList = new ArrayList<String>(3);
prgmNameList.add("bbbb");

That said, if you insist on using an array, you will need to copy your initial array into a new array for each new element that you want to add to the array which can be expensive. See System#arrayCopy for more details. In fact, the ArrayList class internally uses an array that is expanded once it is full using System.arrayCopy so why reinvent the wheel? Just use an ArrayList

Upvotes: 1

Related Questions