Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add an element to int [] array in java [duplicate]

Tags:

java

arrays

Want to add or append elements to existing array

int[] series = {4,2};

now i want to update the series dynamically with new values i send..

like if i send 3 update series as int[] series = {4,2,3};

again if i send 4 update series as int[] series = {4,2,3,4};

again if i send 1 update series as int[] series = {4,2,3,4,1}; so on

How to do it????

I generate an integer every 5 minutes in some other function and want to send to update the int[] series array..

like image 333
Kiran Avatar asked Apr 09 '13 10:04

Kiran


People also ask

How do you add duplicate elements to an array in Java?

You got two options to do this. First, as what @bholagabbar said, use ArrayList instead. Option 2, you can double the array size, copy the old array into the new one (which is similar to what ArrayList does under the hood). BTW, adding one extra space is not a good idea to expand an array.

How do you duplicate elements in an array?

Duplicate elements can be found using two loops. The outer loop will iterate through the array from 0 to length of the array. The outer loop will select an element. The inner loop will be used to compare the selected element with the rest of the elements of the array.

How do you add a value to an array dynamically in Java?

Since the size of an array is fixed you cannot add elements to it dynamically. But, if you still want to do it then, Convert the array to ArrayList object. Add the required element to the array list.


4 Answers

The length of an array is immutable in java. This means you can't change the size of an array once you have created it. If you initialised it with 2 elements, its length is 2. You can however use a different collection.

List<Integer> myList = new ArrayList<Integer>();
myList.add(5);
myList.add(7);

And with a wrapper method

public void addMember(Integer x) {
    myList.add(x);
};
like image 139
flavian Avatar answered Oct 20 '22 09:10

flavian


try this

public static void main(String[] args) {
    int[] series = {4,2};
    series = addElement(series, 3);
    series = addElement(series, 1);
}

static int[] addElement(int[] a, int e) {
    a  = Arrays.copyOf(a, a.length + 1);
    a[a.length - 1] = e;
    return a;
}
like image 20
Evgeniy Dorofeev Avatar answered Oct 20 '22 09:10

Evgeniy Dorofeev


If you are generating an integer every 5 minutes, better to use collection. You can always get array out of it, if required in your code.

Else define the array big enough to handle all your values at runtime (not preferred though.)

like image 27
Sudhanshu Umalkar Avatar answered Oct 20 '22 08:10

Sudhanshu Umalkar


You'll need to create a new array if you want to add an index.

Try this:

public static void main(String[] args) {
    int[] series = new int[0];
    int x = 5;


    series = addInt(series, x);

    //print out the array with commas as delimiters
    System.out.print("New series: ");
    for (int i = 0; i < series.length; i++){
        if (i == series.length - 1){
            System.out.println(series[i]);
        }
        else{
            System.out.print(series[i] + ", ");
        }
    }
}

// here, create a method

public static int[] addInt(int [] series, int newInt){
    //create a new array with extra index
    int[] newSeries = new int[series.length + 1];

    //copy the integers from series to newSeries    
    for (int i = 0; i < series.length; i++){
        newSeries[i] = series[i];
    }
//add the new integer to the last index     
    newSeries[newSeries.length - 1] = newInt;



    return newSeries;

     }
like image 26
Didi Bui Avatar answered Oct 20 '22 09:10

Didi Bui