Object Oriented Programming Articles

Page 247 of 589

Insert an element to List using ListIterator in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 347 Views

Let us first create an ArrayList −ArrayList < Integer > arrList = new ArrayList < Integer > (); arrList.add(100); arrList.add(200); arrList.add(300); arrList.add(400); arrList.add(500);Now, create a ListIterator from the above ArrayList and insert more elements −ListIterator < Integer > iterator = arrList.listIterator(); iterator.add(1000); iterator.add(2000); iterator.add(3000);Exampleimport java.util.ArrayList; import java.util.ListIterator; public class Demo {    public static void main(String[] args) {       ArrayListarrList = new ArrayList();       arrList.add(100);       arrList.add(200);       arrList.add(300);       arrList.add(400);       arrList.add(500);       arrList.add(600);       arrList.add(700);       arrList.add(800);       ListIteratoriterator = arrList.listIterator();       iterator.add(1000);       iterator.add(2000);       iterator.add(3000);       for (Integer i: arrList) {          System.out.println(i);       }    } }output1000 2000 3000 100 200 300 400 500 600 700 800

Read More

Generate 10 random four-digit numbers in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 5K+ Views

To generated random integer, use the Random class with nextInt. At first, create a Random object −Random rand = new Random();The Random above is a random number generator. Now, pick the random numbers one by one. We want 10 random four-digit numbers, therefore loop it until i = 1 to 10 −for (int i = 1; i

Read More

Java Program to generate custom random number -1 or 1

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 288 Views

To generate custom random number 1 or -1, you need to use nextBoolean(). At first take a loop and create a Random object on each iteration −for (int i = 0; i < 5; i++) {    Random rand = new Random(); }Now, use nextBoolean() to generate 1 on TRUE condition, ekse -1 −for (int i = 0; i < 5; i++) {    Random rand = new Random();    if (rand.nextBoolean())       System.out.println(1);    else       System.out.println(-1); }Exampleimport java.util.Random; public class Demo {    public static void main(String[] args) {       for (int ...

Read More

How to generate a random BigInteger value in Java?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 3K+ Views

To generate random BigInteger in Java, let us first set a min and max value −BigInteger maxLimit = new BigInteger("5000000000000"); BigInteger minLimit = new BigInteger("25000000000");Now, subtract the min and max −BigInteger bigInteger = maxLimit.subtract(minLimit); Declare a Random object and find the length of the maxLimit: Random randNum = new Random(); int len = maxLimit.bitLength();Now, set a new B integer with the length and the random object created above.Exampleimport java.math.BigInteger; import java.util.Random; public class Demo {    public static void main(String[] args) {       BigInteger maxLimit = new BigInteger("5000000000000");       BigInteger minLimit = new BigInteger("25000000000");     ...

Read More

How to convert Integer array list to float array in Java?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 1K+ Views

To convert integer array list to float array, let us first create an integer array list −ArrayList < Integer > arrList = new ArrayList < Integer > (); arrList.add(25); arrList.add(50); arrList.add(100); arrList.add(200); arrList.add(300); arrList.add(400); arrList.add(500);Now, convert integer array list to float array. We have first set the size to the float array. With that, each and every value of the integer array is assigned to the float array −final float[] arr = new float[arrList.size()]; int index = 0; for (final Integer value: arrList) {    arr[index++] = value; }Exampleimport java.util.ArrayList; public class Demo {    public static void main(String[] args) ...

Read More

How to generate random values that won't repeat in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 500 Views

To generate random values that won’t repeat, use HashSet collection. Firstly, create a random object and HashSet −Random randNum = new Random(); Sets = new HashSet();Now, add random integers −while (s.size() < 10) {    s.add(randNum.nextInt()); }Now, display the random numbers that are unique −Listlist = new ArrayList(s); System.out.println(list);Exampleimport java.util.ArrayList; import java.util.HashSet; import java.util.Set; import java.util.Random; import java.util.List; public class Demo {    public static void main(String[] args) {       Random randNum = new Random();       Sets = new HashSet();       while (s.size() < 10) {          s.add(randNum.nextInt());       ...

Read More

How can I generate two separate outputs using Random in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 261 Views

To generate two separate outputs, at first create a new Random object −private static final Random r = new Random();Now, let us declare a value −int val = 5;Loop from the value till 100 and generate random numbers between 1 to 100 −while (val

Read More

How to convert Wrapper value array list into primitive array in Java?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 760 Views

Here, to convert Wrapper value array list into primitive array, we are considering Integer as Wrapper, whereas double as primitive.At first, declare an Integer array list and add elements to it −ArrayList < Integer > arrList = new ArrayList < Integer > (); arrList.add(5); arrList.add(10); arrList.add(15); arrList.add(20); arrList.add(25); arrList.add(30); arrList.add(45); arrList.add(50);Now, convert the above Integer array list to primitive array. Firstly, we set the same size for the double array and then assigned each valuefinal double[] arr = new double[arrList.size()]; int index = 0; for (final Integer value: arrList) {    arr[index++] = value; }The following is an example to ...

Read More

Java Program to output fixed number of array elements in a line

Samual Sam
Samual Sam
Updated on 11-Mar-2026 236 Views

To output fixed number of array elements in a line, we should check for a condition and if that is true, we should place System.out.println(); properly to get a line and the process goes on.Here, we will display 5 elements in a line. At first, create a new integer array and add some elements −int[] list = new int[50]; for (int i = 0; i < list.length; i++) {    list[i] = (int)(i + 20); }Now, declare a new variable and initialize it to 0. This variable is checked in a for loop that loops until the length of the ...

Read More

How to populate a 2d array with random alphabetic values from a range in Java?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 1K+ Views

To populate a 2d array with random alphabets, use the Random class. Let us first declare a 2d array −char arr[][] = new char[3][3];Now, in a nested for loop, use the Random class object to get random values on the basis of switch case. Here, our range is 3 i.e. set of 3 alphabets at once −Random randNum = new Random(); for (int i = 0; i < 3; i++) {    for (int j = 0; j < 3; j++) {       int x = randNum.nextInt(3);       switch (x) {          case ...

Read More
Showing 2461–2470 of 5,881 articles
« Prev 1 245 246 247 248 249 589 Next »
Advertisements