Knowee
Questions
Features
Study Tools

Write a program to create a list using Array List.After creation insert a new element at the required position and print the list123452(position)10(new element)output:1210345

Question

Write a program to create a list using Array List.After creation insert a new element at the required position and print the list123452(position)10(new element)output:1210345

🧐 Not the exact question you are looking for?Go ask a question

Solution 1

Here is a simple Java program that accomplishes this:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        // Create an ArrayList
        ArrayList<Integer> numbers = new ArrayList<Integer>();

        // Add elements to the ArrayList
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(4);
        numbers.add(5);

        // Print the original ArrayList
        System.out.println("Original ArrayList: " + numbers);

        // Insert 10 at the 2nd position
        numbers.add(2, 10);

        // Print the updated ArrayList
        System.out.println("Updated ArrayList: " + numbers);
    }
}

In this program, we first create an ArrayList named numbers and add five elements to it. We then print the original ArrayList. After that, we use the add() method to insert the number 10 at the 2nd position (index 2) in the ArrayList. Finally, we print the updated ArrayList.

This problem has been solved

Solution 2

Here is a simple Java program that accomplishes this:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        // Create an ArrayList
        ArrayList<Integer> numbers = new ArrayList<Integer>();

        // Add elements to the ArrayList
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(4);
        numbers.add(5);

        // Print the original ArrayList
        System.out.println("Original ArrayList: " + numbers);

        // Insert 10 at index 2
        numbers.add(2, 10);

        // Print the updated ArrayList
        System.out.println("Updated ArrayList: " + numbers);
    }
}

When you run this program, it first creates an ArrayList and adds the numbers 1, 2, 3, 4, and 5 to it. It then prints the original ArrayList. After that, it inserts the number 10 at index 2 (the third position, since indexes start at 0), and prints the updated ArrayList. The output will be:

Original ArrayList: [1, 2, 3

This problem has been solved

Solution 3

Here is a Java program that accomplishes this:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        // Create an ArrayList
        ArrayList<Integer> numbers = new ArrayList<Integer>();

        // Add elements to the ArrayList
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(4);
        numbers.add(5);

        // Print the original ArrayList
        System.out.println("Original ArrayList: " + numbers);

        // Insert 10 at position 2
        numbers.add(2, 10);

        // Print the updated ArrayList
        System.out.println("Updated ArrayList: " + numbers);
    }
}

In this program, we first create an ArrayList named numbers and add the elements 1, 2, 3, 4, and 5 to it. We then print the original ArrayList. After that, we use the add() method of the ArrayList class to insert the number 10 at position 2 (since ArrayList is 0-indexed, position 2 is the third position). Finally, we print the updated ArrayList.

This problem has been solved

Similar Questions

Write a program in C to insert New value in the array at a given positionNote: First read array size then elements and then position and its valueFor example:Test Input Result151 2 3 4 53 8New Array is 1 2 8 3 4 5

Create an integer array of size 10, where all the values should be 0 but the fifth value should be 1.Print the elements of array.

Write a program to print the following output: [3] 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1

Create a list num=[23,12,5,9,65,44].➢ Print the length of the list.➢ Print the elements from second to fourth position using positive indexing.➢ Print the elements from position third to fifth using negative indexing

Predict the output?       int arr[] = {10, 20, 30, 40, 50};       for(int i=0; i < arr.length; i++)       {             System.out.print(" " + arr[i]);                     }

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.