Tutorialspoint

Java Program to get minimum value with Comparator

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Demo {
	@SuppressWarnings("unchecked")
	public static void main(String args[]) {
		Integer arr[] = { 40, 20, 30, 10, 90, 60, 700 };
		List list = Arrays.asList(arr);
		Comparator comp = Collections.reverseOrder();
		System.out.println("Minimum element = "+Collections.max(list, comp));
	}
}

Java Program to generate random elements from a given array

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Demo {
	public static void main(String[] args) {
		Integer[] arr = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
		System.out.print("Array elements...\n");
		for (Integer res: arr) {
			System.out.print(res + " ");
		}
		List<Integer>list = Arrays.asList(arr);
		Collections.shuffle(list);
		System.out.println("\n\nRandom elements...");
		for (Integer res: list) {
			System.out.print(res + " ");
		}
	}
}

Get min and max value in Java

import java.util.Random;
public class Demo {
	public static void main(String[] args) {
		double[] val = new double[10];
		double min = Double.MAX_VALUE;
		double max = Double.MIN_VALUE;
		System.out.println("Random number array...");
		for (int i = 0; i<val.length; i++) {
			val[i] = new Random().nextInt(100);
			System.out.println(val[i]);
		}
		for (int i = 0; i<val.length; i++) {
			if (val[i] <min)
				min = val[i];
			if (val[i] >max)
				max = val[i];
		}
		System.out.println("Minimum element = " + min);
		System.out.println("Maximum element = " + max);
	}
}

Java Program to shift array elements to the right

import java.util.Arrays;
public class Demo {
	public static void main(String[] argv) throws Exception {
		int[] arr = { 10, 20, 30, 40, 50, 60, 70, 80, 90 };
		System.out.println("Initial array...\n"+Arrays.toString(arr));
		System.arraycopy(arr, 0, arr, 1, arr.length - 1);
		System.out.println("Array after shifting to the right...");
		System.out.println(Arrays.toString(arr));
	}
}

shuffle an array in Java

import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class Demo {
	public static void main(String[] args) {
		String[] letters = { "P", "Q", "R", "S", "T", "U","V", "W", "X", "Y", "Z"  };
		System.out.println("Initial array = "+Arrays.toString(letters));
		List<String>list = Arrays.asList(letters);
		int len = list.size();
		System.out.println("Shuffled array...");
		for (int i = 0; i<letters.length; i++) {
			int index = new Random().nextInt(len);
			String shuffle = list.get(index);
			System.out.println(shuffle);
		}
	}
}

Randomize and shuffle array of numbers in Java

import java.util.Arrays;
import java.util.Random;
public class Demo {
	public static void main(String[] args) {
		int[] arr = { 20, 40, 60, 80,100, 120, 140, 160, 180, 200};
		System.out.println("The integer array = "+Arrays.toString(arr));
		Random rand = new Random();
		for(int i = 0; i<arr.length; ++i) {
			int index = rand.nextInt(arr.length - i);
			int tmp = arr[arr.length - 1 - i];
			arr[arr.length - 1 - i] = arr[index];
			arr[index] = tmp;
		}
		System.out.println("Shuffle integer array = "+Arrays.toString(arr));
	}
}

Java Program to create an array with randomly shuffled numbers

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Demo {
	public static void main(String args[]) {
		List<Integer>list = new ArrayList<Integer>();
		list.add(10);
		list.add(50);
		list.add(100);
		list.add(150);
		list.add(200);
		list.add(350);
		list.add(500);
		list.add(800);
		list.add(1000);
		Collections.shuffle(list);
		int len = list.size();
		int[] res = new int[len];
		for (int i = 0; i<len; i++) {
			res[i] = list.get(i);
		}
		System.out.println("Randomly shuffled array elements = "+Arrays.toString(res));
	}
}

Java Program to get maximum value with Comparator

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Demo {
	@SuppressWarnings("unchecked")
	public static void main(String args[]) {
		Integer arr[] = { 40, 20, 30, 10, 90, 60, 70 };
		List list = Arrays.asList(arr);
		Comparator comp = Collections.reverseOrder();
		System.out.println("Maximum element = "+Collections.min(list, comp));
	}
}

Java Program to extend the size of an Integer array

public class Demo {
	public static void main(String[] args) {
		Integer[] arr = new Integer[] { 50, 100, 150, 200, 400, 500, 800, 1000};
		System.out.println("All the elements before extending the array...");
		for (Integer i:arr)
			System.out.println(i);
			Integer[] new_size = new Integer[15];
			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;
			System.arraycopy(arr, 0, new_size, 0, arr.length);
			System.out.println("All the elements after extending the array...");
			for (Integer i: new_size)
				System.out.println(i);
	}
}

C/C++ Address zero used for the null pointer

cpp

#include <stdio.h>
int main() {
   int *p= NULL;//initialize the pointer as null.
   printf("The value of pointer is %u",p);
   return 0;
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.