Play with Java Arrays & Patterns
Arrays & Patterns — Sounds interesting? or complicated? Let’s find out..
There are lots of resources available online and I thought to put all together for the ease of use. Hope it will help you too :)
A simple online compiler will help you to try out : https://www.programiz.com/java-programming/online-compiler/
Basics on How to..
- Declare an array
int[] a = new int[]{45, 12, 78, 34, 89, 21}; //method 1
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; //method 2
- Print an array
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars[0]); //will print the value of 0th index
//output : Volvo
- Get Array length
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars.length);
//output : 4
Here are some interesting and commonly practiced algorithms
- Sort an Integer Array using “Arrays” util
import java.util.Arrays;
public class MyClass {
public static void main(String args[]) {
int[] a = new int[] {45,12,78,34,89,21};
Arrays.sort(a);
System.out.print(Arrays.toString(a));
}
}
//output : [12, 21, 34, 45, 78, 89]
- Reverse a string input using “Scanner” util
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Enter string to reverse:");
Scanner read = new Scanner(System.in);
String str = read.nextLine();
String reverse = "";
for (int i = str.length() - 1; i >= 0; i--) {
reverse = reverse + str.charAt(i);
}
System.out.println("Reversed string is:");
System.out.println(reverse);
}
}
/* output:
Enter string to reverse:
Automation
Reversed string is:noitamotuA
*/
- Reverse String without using utils
String rev = "";
String str = "Automation";
for (int i = str.length(); i > 0; --i) {
rev = rev + (str.charAt(i - 1));
}
System.out.print(rev);
//output: noitamotuA
- Reverse an Int array
int[] arr = {10,20,30,40,50};
int[] b = new int[arr.length];
int j = arr.length;
for (int i = 0; i < arr.length; i++) {
b[j - 1] = arr[i];
j = j - 1;
}
// printing the reversed array
for (int k = 0; k < arr.length; k++) {
System.out.print(b[k] + " ");
}
//output: 50 40 30 20 10
- Print Max number
int[] myNum = {2,1,10,5,3,8};
int max = 0;
for (int i = 0; i < myNum.length; i++) {
if (myNum[i] > max) {
max = myNum[i];
}
}
System.out.print(max);
//output: 10
- Return true if number is present
int[] myNum = {2,1,10,5,3,8};
int a = 99; // Enter the value to check
boolean result = false;
for (int i = 0; i < myNum.length; i++) {
if (myNum[i] == a) {
result = true;
break;
}
}
System.out.print(result);
//output: false
- Print the multiplication for 10 items for given int
int c = 10;
int i = c++;
for (int a = 1; a <= 10; a++) {
int j = i * a;
System.out.println(i + "*" + a + "=" + j);
}
/* output:
Enter number:10
10*1=10
10*2=20
10*3=30
10*4=40
10*5=50
10*6=60
10*7=70
10*8=80
10*9=90
10*10=100
*/
- Print the multiplication for 10 items for given int using “Scanner” util
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Enter number:");
Scanner in = new Scanner(System.in);
int i = in.nextInt();
for (int a = 1; a <= 10; a++) {
int j = i * a;
System.out.println(i + "*" + a + "=" + j);
}
}
}
/* output:
Enter number:10
10*1=10
10*2=20
10*3=30
10*4=40
10*5=50
10*6=60
10*7=70
10*8=80
10*9=90
10*10=100
*/
- Find the number of occurrences
String text = "automation";
char someChar = 'a'; //Enter the char
int count = 0;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == someChar) {
count++;
}
}
System.out.println(count + " Occurrences found for the word '" + text + "'");
//output: 2 Occurrences found for the word 'automation'
- Find duplicates in an Array
int[] arr = new int[] {1,2,5,5,6,6,7,2};
for (int a = 0; a < arr.length; a++) {
for (int i = a + 1; i < arr.length; i++) {
if (arr[a] == arr[i])
System.out.println("Duplicate Element :" + arr[a]);
}
}
//output
Duplicate Element :2
Duplicate Element :5
Duplicate Element :6
- Largest number in an Array
int[] myNum = {2,1,10,5,3,8};
int largest = 0;
for (int n: myNum) {
if (n > largest) {
largest = n;
}
}
System.out.println("Largest :" + largest);
//output: Largest :10
- Second largest in an Array
int[] myNum = {2,1,10,5,3,8};
int largest = 0;
int secondLargest = 0;
for (int n: myNum) {
if (n > largest) {
secondLargest = largest;
largest = n;
} else if (n > secondLargest) {
secondLargest = n;
}
}
System.out.println("Largest :" + largest);
System.out.println("Second Largest :" + secondLargest);
//output: Largest :10 Second Largest :8
- Remainder
int[] myNum = {2,1,10,5,3,8};
int remainder;
for (int i = 0; i < myNum.length; i++) {
remainder = myNum[i] % 2;
System.out.println("Remainder :" + remainder);
}
//output:
Remainder :0
Remainder :1
Remainder :0
Remainder :1
Remainder :1
Remainder :0
- Star Pattern
for (int i = 0; i <= 5; i++) {
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println("");
}
//output
*
**
***
****
*****
- Number Pattern
for (int i = 0; i <= 5; i++) {
for (int j = 0; j < i; j++) {
System.out.print(i);
}
System.out.println();
}
//output
1
22
333
4444
55555
There are many more but will stop here for now :) Leave a comment if there’s something I’ve missed. Love to hear from you..
Stay tuned for my next article on “Printing Patterns in Java”.
Happy Learning!