Found 4336 Articles for Java 8

Java Program to extend the size of an Integer array

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

228 Views

Let us first create an Integer array −Integer[] arr = new Integer[] { 50, 100, 150, 200, 400, 500, 800, 1000};Now, create a new array with the extended size −Integer[] new_size = new Integer[15];Add elements for the extended size −new_size[8] = 2000; new_size[9] = 3000; new_size[10] = 4000; new_size[11] = 5000; new_size[12] = 6000; new_size[13] = 9000; new_size[14] = 10000;Now, copy the array and form it the final array with the extended size −System.arraycopy(arr, 0, new_size, 0, arr.length);Example Live Demopublic class Demo {    public static void main(String[] args) {       Integer[] arr = new Integer[] { 50, 100, ... Read More

How can we copy one array from another in Java

Samual Sam
Updated on 30-Jul-2019 22:30:25

303 Views

Let us first create a string array −String[] arr = new String[] { "P", "Q", "R", "S", "T"};Now, calculate the length of the above array and create a new array with the same length −int len = arr.length; String[] arr2 = new String[len];Let us copy one array from another −System.arraycopy(arr, 0, arr2, 0, arr.length);Example Live Demopublic class Demo {    public static void main(String[] args) {       String[] arr = new String[] { "P", "Q", "R", "S", "T"};       System.out.println("Initial array...");       for (int i = 0; i < arr.length; i++)       System.out.println(arr[i]); ... Read More

Java Program to convert array to String for one dimensional and multi-dimensional arrays

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

284 Views

For converting array to 1D and 2D arrays, let us first create a one-dimensional and two-dimensional array −One-DimensionalString str[] = {"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};Two-Dimensionaldoubled [][]= {    {1.2, 1.3, 2.1, 4.1},    {1.5, 2.3},    {2.5, 4.4},    {3.8},    {4.9},    {3.2, 2.1, 3.2, 7.2} };Converting array to string for one-dimensional array −Arrays.toString(str);Converting array to string for two-dimensional array −Arrays.deepToString(d);Exampleimport java.util.Arrays; public class Demo {    public static void main(String args[]) {       String str[] = {"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};       ... Read More

Java program to sort integers in unsorted array

Samual Sam
Updated on 28-Jun-2024 18:19:18

2K+ Views

Sorting refers to arranging data in a particular format. Sorting algorithm specifies the way to arrange data in a particular order (ascending or descending). Problem Statement For a given array write Java program to sort integers in unsorted array. Consider the following example - Input The unsorted integer array = [10, 14, 28, 11, 7, 16, 30, 50, 25, 18] Output The unsorted integer array = [10, 14, 28, 11, 7, 16, 30, 50, 25, 18] The sorted integer array = [7, 10, 11, 14, 16, 18, 25, 28, 30, 50] Sorting Integers in Unsorted Array ... Read More

Java Program to sort a subset of array elements

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

224 Views

Let us first create a string array −String[] strArr = new String[] { "r", "p", "v", "y", "s", "q" };Now, use Arrays.sort() to get the subset. Use the following to sort only from the index range 2 to 6.Arrays.sort(strArr, 2, 6);Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       String[] strArr = new String[] { "r", "p", "v", "y", "s", "q" };       Arrays.sort(strArr, 2, 6);       System.out.println("Sorted subset of array elements from index 2 to 6...");       for (int a = 0; a < strArr.length; ... Read More

Java Program to calculate the time of sorting an array

Samual Sam
Updated on 30-Jul-2019 22:30:25

561 Views

To calculate the time of sorting an array, let us first create an array and add elements to it −int[] arr = new int[1000]; for (int i = 0; i < arr.length; i++) {    arr[i] = (int)(i + 20); }Now, set tow Date variables i.e. for past and future to get the time of sorting. Between both the dates, use sort() to sort the array −Date past = new Date(); Arrays.sort(arr); Date future = new Date();Get the difference to calculate the time of sorting −(future.getTime() - past.getTime()Example Live Demoimport java.util.Arrays; import java.util.Date; public class Demo {    public static void ... Read More

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

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

992 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

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

Samual Sam
Updated on 30-Jul-2019 22:30:25

148 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 convert Wrapper value array list into primitive array in Java?

Samual Sam
Updated on 30-Jul-2019 22:30:25

498 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

How can I generate two separate outputs using Random in Java

Samual Sam
Updated on 30-Jul-2019 22:30:25

141 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

Advertisements