How to Dynamically Add Items to a Java Array

How can I dynamically add items to a Java array?

Look at java.util.LinkedList or java.util.ArrayList

List<Integer> x = new ArrayList<Integer>();
x.add(1);
x.add(2);

How to dynamically add elements to String array?

Arrays in Java have a defined size, you cannot change it later by adding or removing elements (you can read some basics here).

Instead, use a List:

ArrayList<String> mylist = new ArrayList<String>();
mylist.add(mystring); //this adds an element to the list.

Of course, if you know beforehand how many strings you are going to put in your array, you can create an array of that size and set the elements by using the correct position:

String[] myarray = new String[numberofstrings];
myarray[23] = string24; //this sets the 24'th (first index is 0) element to string24.

Java - Add and remove elements from a dynamic array/list

You can try to use label.
So basically the break in your case 3 only breaks outside switch statement but not outside the do while loop.
You can add a statement before do as below:

outerloop:
do{
switch(a){
// rest of your logic
case 3:
break outerloop;
}
}
while(true)

where outerloop is a label. Please try this.

Adding values to array dynamically

You can't add to an array. Once you've created it, its size is fixed. You'd be better off using an ArrayList, and just calling add.

List<String> newMembers = new ArrayList<String>();
for (MyClass entry : contactClassList) {
if (entry.isChecked) {
newMembers.add(entry.getUsernameEmail());
}
}
System.out.println(newMember.size());

Dynamically create an integer array

According to your question if want to create dynamic size array then use

int array[]=new int[size_of_array];

And if create ArrayList then it is already a dynamic size no need to specified size.

ArrayList<Integer> array = new ArrayList<>(size_of_array);

if you do this then you are defining capacity of ArrayList which is auto increment when initial capacity is full.

and when you add elements in your list like

array.add(45);

then its size increase and it not show you 0.

append values dynamically into an long[] array

You can not "append" elements to an array in Java. The length of the array is determined at the point of creation and can't change dynamically.

If you really need a long[] the solution is to create a larger array, copy over the elements, and point the reference to the new array, like this:

long[] s = new long[] {0, 1, 2};
long[] toAppend = { 3, 4, 5 };

long[] tmp = new long[s.length + toAppend.length];
System.arraycopy(s, 0, tmp, 0, s.length);
System.arraycopy(toAppend, 0, tmp, s.length, toAppend.length);

s = tmp; // s == { 0, 1, 2, 3, 4, 5 }

However, you probably want to use an ArrayList<Long> for this purpose. In that case you can append elements using the .add-method. If you choose this option, it should look something like

// Initialize with 0, 1, 2
ArrayList<Long> s = new ArrayList<Long>(Arrays.asList(0L, 1L, 2L));

// Append 3, 4, 5
s.add(3L);
s.add(4L);
s.add(5L);

long[] longArray = new long[s.size()];
for (int i = 0; i < s.size(); i++)
longArray[i] = s.get(i);

Add items dynamically to ListInteger and convert to Integer[] Array

The first and foremost problem with your code is that you are looking over the components array and there is no such field defined in your method but instead according to your problem statement it should be

switch (s[i])

And then there is also problem with your switch statement as they end without the break statement making them run for all cases whether or not it is the case.
You can better understand by my implementation of your code which looks like

import java.io.*;
import java.util.*;
class Test
{
public static void main(String args[])throws IOException
{

List<Integer> I = new ArrayList<Integer>();
String[] s = {"Light", "Sun", "Sun", "Water"};
String[] ss = {"on", "off", "on", "off"};
for(int i = 0; i < s.length; i++) {
switch (s[i]) {
case "Light":
if (ss[i]=="on")
I.add(1);
else
I.add(2);
case "Sun":
if (ss[i]=="on")
I.add(3);
else
I.add(4);
case "Water":
if (ss[i]=="on")
I.add(5);
else
I.add(6);
case "Gravel":
if (ss[i]=="on")
I.add(7);
else
I.add(8);
}
System.out.println(I+" at i="+i);
}
Integer[] arr = I.toArray(new Integer[I.size()]);
System.out.println(I);
}
}

So here is the code you have written and when I run it I get the output

[1, 3, 5, 7] at i=0
[1, 3, 5, 7, 4, 6, 8] at i=1
[1, 3, 5, 7, 4, 6, 8, 3, 5, 7] at i=2
[1, 3, 5, 7, 4, 6, 8, 3, 5, 7, 6, 8] at i=3
[1, 3, 5, 7, 4, 6, 8, 3, 5, 7, 6, 8]

So you can see that always all case are run and based on the value of ss[] the output arrives.
So you can correct the code as

import java.io.*;
import java.util.*;
class Test
{
public static void main(String args[])throws IOException
{

List<Integer> I = new ArrayList<Integer>();
String[] s = {"Light", "Sun", "Sun", "Water"};
String[] ss = {"on", "off", "on", "off"};
for(int i = 0; i < s.length; i++) {
switch (s[i]) {
case "Light":
if (ss[i]=="on")
I.add(1);
else
I.add(2);
break;
case "Sun":
if (ss[i]=="on")
I.add(3);
else
I.add(4);
break;
case "Water":
if (ss[i]=="on")
I.add(5);
else
I.add(6);
break;
case "Gravel":
if (ss[i]=="on")
I.add(7);
else
I.add(8);
break;
}
System.out.println(I+" at i="+i);
}
Integer[] arr = I.toArray(new Integer[I.size()]);
System.out.println(I);
}
}

And the output would be

[1] at i=0
[1, 4] at i=1
[1, 4, 3] at i=2
[1, 4, 3, 6] at i=3
[1, 4, 3, 6]

Which is the required answer.
Hope I have made myself clear.



Related Topics



Leave a reply



Submit