Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses
Showing posts with label Arrays in Java. Show all posts
Showing posts with label Arrays in Java. Show all posts

Animation - How to reverse an array in Java ?



Video tutorial to demonstrate how to reverse an array in Java










For full course at 90% off visit link - Visualizing Data Structures and Algorithms in Java

Want to land a software engineering job in the IT industry? This course is here to help. The course walks you through multiple algorithms, data structures problems and their solutions with step by step visualizations, so that you are actually learning instead of blindly memorizing solutions.

The course covers in and outs of Data Structures and Algorithms in Java. The implementation of various Algorithms and Data Structures have been demonstrated and implemented through animated slides. It covers most of the interview room questions on Algorithms and Data Structures. The questions and solutions are demonstrated by -

1. Animated slide. (To make visualization of algorithms faster)

2. Coding algorithm on IDE.








Binary Search in Java (video tutorial)



Video tutorial to demonstrate Binary Search in Java










For full course at 90% off visit link - Visualizing Data Structures and Algorithms in Java

Want to land a software engineering job in the IT industry? This course is here to help. The course walks you through multiple algorithms, data structures problems and their solutions with step by step visualizations, so that you are actually learning instead of blindly memorizing solutions.

The course covers in and outs of Data Structures and Algorithms in Java. The implementation of various Algorithms and Data Structures have been demonstrated and implemented through animated slides. It covers most of the interview room questions on Algorithms and Data Structures. The questions and solutions are demonstrated by -

1. Animated slide. (To make visualization of algorithms faster)

2. Coding algorithm on IDE.






How to solve Two Sum problem in Java ? (Video tutorial)



Video tutorial to demonstrate how to solve Two Sum problem in Java.










For full course at 90% off visit link - Visualizing Data Structures and Algorithms in Java

Want to land a software engineering job in the IT industry? This course is here to help. The course walks you through multiple algorithms, data structures problems and their solutions with step by step visualizations, so that you are actually learning instead of blindly memorizing solutions.

The course covers in and outs of Data Structures and Algorithms in Java. The implementation of various Algorithms and Data Structures have been demonstrated and implemented through animated slides. It covers most of the interview room questions on Algorithms and Data Structures. The questions and solutions are demonstrated by -

1. Animated slide. (To make visualization of algorithms faster)

2. Coding algorithm on IDE.






How to insert a node in a sorted Singly Linked List in Java ? (Video tutorial)



Video tutorial to demonstrate how to insert a node in a sorted Singly Linked List in Java.










For full course at 90% off visit link - Visualizing Data Structures and Algorithms in Java

Want to land a software engineering job in the IT industry? This course is here to help. The course walks you through multiple algorithms, data structures problems and their solutions with step by step visualizations, so that you are actually learning instead of blindly memorizing solutions.

The course covers in and outs of Data Structures and Algorithms in Java. The implementation of various Algorithms and Data Structures have been demonstrated and implemented through animated slides. It covers most of the interview room questions on Algorithms and Data Structures. The questions and solutions are demonstrated by -

1. Animated slide. (To make visualization of algorithms faster)

2. Coding algorithm on IDE.







How to convert String to primitive char array and print it on the console through a Java program ?.

Program to demonstrate how to convert String to primitive char array and print it on the console through a Java program.


package com.hubberspot.examples;

public class StringToCharArrayDemo {

 public static void main(String[] args) {

  // Create a String object with any value
  String welcome = "Hello !!!. Welcome to Hubberspot.com !!!.";

  // Print the String on the console
  System.out.println("String used is : " + welcome);

  // In order to convert String to char array - 
  // String class has a method by name as : toCharArray()
  // This method returns back an array which holds characters
  // of String.
  char[] message = welcome.toCharArray();

  System.out.println("\nString converted to char array : ");

  // Looping over each character in message array created above.
  // Printing each character one by one.
  for(char character : message) {

   System.out.print(character);

  }
 }
}




Output of the program : 


 

How to perform Binary Search for an element over primitive arrays in Java ?

Program to demonstrate how to perform Binary Search for an element over primitive arrays in Java

package com.hubberspot.example;

import java.util.Arrays;

public class PrimitiveArrayBinarySearch {

   public static void main(String[] args) {

 byte[] b = new byte [] { 3, 6, 7, 2, 1 };
 short[] s = new short[] { 9, 5, 4, 3, 2 };
 int[] i = new int[] { 34, 12, 1, 23, 78 };
 long[] l = new long[] { 100, 99, 45, 23, 1 };
 float[] f = new float[] { 3.0f, 6.1f, 7.6f, 2.5f, 1.1f };
 double[] d = new double[] { 3.6, 6.4, 7.2, 2.7, 1.8 };
 char[] c = new char[] {'e','g','s','a','c'};

 System.out.println("Original Arrays are as : ");
 System.out.println("-----------------------------------------");

 System.out.print("byte array : [");
 for(int j = 0; j < 5; j++) {

  System.out.print(b[j] + " ");

 }
 System.out.println("]");
 System.out.println("-----------------------------------------");

 System.out.print("short array : [");
 for(int j = 0; j < 5; j++) {

  System.out.print(s[j] + " ");

 }
 System.out.println("]");
 System.out.println("-----------------------------------------");

 System.out.print("int array : [");
 for(int j = 0; j < 5; j++) {

  System.out.print(i[j] + " ");

 }
 System.out.println("]");
 System.out.println("-----------------------------------------");

 System.out.print("long array : [");
 for(int j = 0; j < 5; j++) {

  System.out.print(l[j] + " ");

 }
 System.out.println("]");
 System.out.println("-----------------------------------------");

 System.out.print("float array : [");
 for(int j = 0; j < 5; j++) {

  System.out.print(f[j] + " ");

 }
 System.out.println("]");
 System.out.println("-----------------------------------------");

 System.out.print("double array : [");
 for(int j = 0; j < 5; j++) {

  System.out.print(d[j] + " ");

 }
 System.out.println("]");
 System.out.println("-----------------------------------------");

 System.out.print("char array : [");
 for(int j = 0; j < 5; j++) {

  System.out.print(c[j] + " ");

 }
 System.out.println("]");
 System.out.println("-----------------------------------------");


 System.out.println();
 System.out.println();

 // Sorting the primitives array using Arrays.sort()
 // Its important to sort elements before doing binary 
 // search over each elements
 Arrays.sort(b);
 Arrays.sort(s);
 Arrays.sort(i);
 Arrays.sort(l);
 Arrays.sort(f);
 Arrays.sort(d);
 Arrays.sort(c);

 // creating valid searches below : 
 byte searchByteKey = 1;
 short searchShortKey = 4;
 int searchIntKey = 1;
 long searchLongKey = 45;
 float searchFloatKey = 6.1f;
 double searchDoubleKey = 6.4;
 char searchCharKey = 'a';

 System.out.println();
 System.out.println("After performing BinarySearch of valid keys : ");
 System.out.println("-----------------------------------------");

 int byteResult = Arrays.binarySearch(b,searchByteKey);
 System.out.println("Result of binary search of 1 is : " + byteResult);
 System.out.println("-----------------------------------------");

 int shortResult = Arrays.binarySearch(s,searchShortKey);
 System.out.println("Result of binary search of 4 is : " + shortResult);
 System.out.println("-----------------------------------------");

 int intResult = Arrays.binarySearch(i,searchIntKey);
 System.out.println("Result of binary search of 1 is : " + intResult);
 System.out.println("-----------------------------------------");

 int longResult = Arrays.binarySearch(l,searchLongKey);
 System.out.println("Result of binary search of 45 is : " + longResult);
 System.out.println("-----------------------------------------");

 int floatResult = Arrays.binarySearch(f,searchFloatKey);
 System.out.println("Result of binary search of 6.1f is : " + floatResult);
 System.out.println("-----------------------------------------");

 int doubleResult = Arrays.binarySearch(d,searchDoubleKey);
 System.out.println("Result of binary search of 6.4 is : " + doubleResult);
 System.out.println("-----------------------------------------");

 int charResult = Arrays.binarySearch(c,searchCharKey);
 System.out.println("Result of binary search of 'a' is : " + charResult);
 System.out.println("-----------------------------------------");

 // creating invalid search keys below : 
 searchByteKey = 20;
 searchShortKey = 40;
 searchIntKey = 25;
 searchLongKey = 62;
 searchFloatKey = 9.0f;
 searchDoubleKey = 99.0;
 searchCharKey = 'z';

 System.out.println();
 System.out.println("After performing BinarySearch of invalid keys : ");
 System.out.println("-----------------------------------------");

 byteResult = Arrays.binarySearch(b,searchByteKey);
 System.out.println("Result of binary search of 20 is : " + byteResult);
 System.out.println("-----------------------------------------");

 shortResult = Arrays.binarySearch(s,searchShortKey);
 System.out.println("Result of binary search of 40 is : " + shortResult);
 System.out.println("-----------------------------------------");

 intResult = Arrays.binarySearch(i,searchIntKey);
 System.out.println("Result of binary search of 25 is : " + intResult);
 System.out.println("-----------------------------------------");

 longResult = Arrays.binarySearch(l,searchLongKey);
 System.out.println("Result of binary search of 62 is : " + longResult);
 System.out.println("-----------------------------------------");

 floatResult = Arrays.binarySearch(f,searchFloatKey);
 System.out.println("Result of binary search of 9.0f is : " + floatResult);
 System.out.println("-----------------------------------------");

 doubleResult = Arrays.binarySearch(d,searchDoubleKey);
 System.out.println("Result of binary search of 99.0 is : " + doubleResult);
 System.out.println("-----------------------------------------");

 charResult = Arrays.binarySearch(c,searchCharKey);
 System.out.println("Result of binary search of 'z' is : " + charResult);
 System.out.println("-----------------------------------------");



   }

}


Output of the program : 




 



Video tutorial to demonstrate how to perform Binary Search in Java.








How to sort and partial sort a primitive arrays using Arrays.sort method in Java ?.

Program to demonstrate How to sort and partial sort a primitive arrays using Arrays.sort method in Java .

package com.hubberspot.example;

import java.util.Arrays;

public class PrimitiveArraySort {

 public static void main(String[] args) {

  byte[] b = new byte [] { 3, 6, 7, 2, 1 };
  short[] s = new short[] { 9, 5, 4, 3, 2 };
  int[] i = new int[] { 34, 12, 1, 23, 78 };
  long[] l = new long[] { 100, 99, 45, 23, 1 };
  float[] f = new float[] { 3.0f, 6.1f, 7.6f, 2.5f, 1.1f };
  double[] d = new double[] { 3.6, 6.4, 7.2, 2.7, 1.8 };
  char[] c = new char[] {'e','g','s','a','c'};

  System.out.println("Original Arrays are as : ");
  System.out.println("-----------------------------------------");

  System.out.print("byte array : [");
  for(int j = 0; j < 5; j++) {

   System.out.print(b[j] + " ");

  }
  System.out.println("]");
  System.out.println("-----------------------------------------");

  System.out.print("short array : [");
  for(int j = 0; j < 5; j++) {

   System.out.print(s[j] + " ");

  }
  System.out.println("]");
  System.out.println("-----------------------------------------");

  System.out.print("int array : [");
  for(int j = 0; j < 5; j++) {

   System.out.print(i[j] + " ");

  }
  System.out.println("]");
  System.out.println("-----------------------------------------");

  System.out.print("long array : [");
  for(int j = 0; j < 5; j++) {

   System.out.print(l[j] + " ");

  }
  System.out.println("]");
  System.out.println("-----------------------------------------");

  System.out.print("float array : [");
  for(int j = 0; j < 5; j++) {

   System.out.print(f[j] + " ");

  }
  System.out.println("]");
  System.out.println("-----------------------------------------");

  System.out.print("double array : [");
  for(int j = 0; j < 5; j++) {

   System.out.print(d[j] + " ");

  }
  System.out.println("]");
  System.out.println("-----------------------------------------");

  System.out.print("char array : [");
  for(int j = 0; j < 5; j++) {

   System.out.print(c[j] + " ");

  }
  System.out.println("]");
  System.out.println("-----------------------------------------");


  System.out.println();
  System.out.println();

  // Sorting the primitives array using Arrays.sort()
  Arrays.sort(b);
  Arrays.sort(s);
  Arrays.sort(i);
  Arrays.sort(l);
  Arrays.sort(f);
  Arrays.sort(d);
  Arrays.sort(c);

  System.out.println("Sorted Arrays after Arrays.sort() method : ");

  System.out.println("-----------------------------------------");

  System.out.print("byte array : [");
  for(int j = 0; j < 5; j++) {

   System.out.print(b[j] + " ");

  }
  System.out.println("]");
  System.out.println("-----------------------------------------");

  System.out.print("short array : [");
  for(int j = 0; j < 5; j++) {

   System.out.print(s[j] + " ");

  }
  System.out.println("]");
  System.out.println("-----------------------------------------");

  System.out.print("int array : [");
  for(int j = 0; j < 5; j++) {

   System.out.print(i[j] + " ");

  }
  System.out.println("]");
  System.out.println("-----------------------------------------");

  System.out.print("long array : [");
  for(int j = 0; j < 5; j++) {

   System.out.print(l[j] + " ");

  }
  System.out.println("]");
  System.out.println("-----------------------------------------");

  System.out.print("float array : [");
  for(int j = 0; j < 5; j++) {

   System.out.print(f[j] + " ");

  }
  System.out.println("]");
  System.out.println("-----------------------------------------");

  System.out.print("double array : [");
  for(int j = 0; j < 5; j++) {

   System.out.print(d[j] + " ");

  }
  System.out.println("]");
  System.out.println("-----------------------------------------");

  System.out.print("char array : [");
  for(int j = 0; j < 5; j++) {

   System.out.print(c[j] + " ");

  }
  System.out.println("]");
  System.out.println("-----------------------------------------");


  System.out.println();
  System.out.println();

  b = new byte [] { 3, 6, 7, 2, 1 };
  s = new short[] { 9, 5, 4, 3, 2 };
  i = new int[] { 34, 12, 1, 23, 78 };
  l = new long[] { 100, 99, 45, 23, 1 };
  f = new float[] { 3.0f, 6.1f, 7.6f, 2.5f, 1.1f };
  d = new double[] { 3.6, 6.4, 7.2, 2.7, 1.8 };
  c = new char[] {'e','g','s','a','c'};

  Arrays.sort(b , 2 , 5);
  Arrays.sort(s , 2 , 5);
  Arrays.sort(i , 2 , 5);
  Arrays.sort(l , 2 , 5);
  Arrays.sort(f , 2 , 5);
  Arrays.sort(d , 2 , 5);
  Arrays.sort(c , 2 , 5);

  System.out.println("Partially sorted Arrays after \nArrays.sort(primitive[] p, int startIndex, int endIndex) method : ");

  System.out.println("-----------------------------------------");

  System.out.print("byte array : [");
  for(int j = 0; j < 5; j++) {

   System.out.print(b[j] + " ");

  }
  System.out.println("]");
  System.out.println("-----------------------------------------");

  System.out.print("short array : [");
  for(int j = 0; j < 5; j++) {

   System.out.print(s[j] + " ");

  }
  System.out.println("]");
  System.out.println("-----------------------------------------");

  System.out.print("int array : [");
  for(int j = 0; j < 5; j++) {

   System.out.print(i[j] + " ");

  }
  System.out.println("]");
  System.out.println("-----------------------------------------");

  System.out.print("long array : [");
  for(int j = 0; j < 5; j++) {

   System.out.print(l[j] + " ");

  }
  System.out.println("]");
  System.out.println("-----------------------------------------");

  System.out.print("float array : [");
  for(int j = 0; j < 5; j++) {

   System.out.print(f[j] + " ");

  }
  System.out.println("]");
  System.out.println("-----------------------------------------");

  System.out.print("double array : [");
  for(int j = 0; j < 5; j++) {

   System.out.print(d[j] + " ");

  }
  System.out.println("]");
  System.out.println("-----------------------------------------");

  System.out.print("char array : [");
  for(int j = 0; j < 5; j++) {

   System.out.print(c[j] + " ");

  }
  System.out.println("]");
  System.out.println("-----------------------------------------");


  System.out.println();
  System.out.println();

 }

}


Output of the program : 




 

How to demonstrate working of Multidimensional Arrays in Java

What do we mean by  Multi-Dimensional Arrays in Java ?. 
Multi-Dimensional Arrays in Java are termed as arrays of arrays. Normal arrays are objects in Java, while if we talk about Multi-Dimensional arrays, those are array objects whose elements themselves store array objects. Two Dimensional arrays are basic representation of tables having rows and columns. Thus, the information stored in two dimensional array is generally in rows and columns. Let us look into a simple program that demonstrate Multidimensional Arrays in Java :- 

package com.hubberspot.multidimensional.array.example; 

public class TwoDimensionalArrayDemo {
  public static void main(String args[]) {
     int twoDArray[][]= new int[5][5];
     int i, j, k = 0;

     for(i=0; i<5; i++)
       for(j=0; j<5; j++) {
         twoDArray[i][j] = k;
         k++;
     }
        
     for(i=0; i<5; i++) {
       for(j=0; j<5; j++)
         System.out.print(twoDArray[i][j] + " ");
         System.out.println();
     }
  }
} 

Output of the program :- 


How to declare, create and access a one-dimensional Array in Java

Introduction :-
Today I will be explaining you some of the basic concepts related to an array. I will be teaching you how to declare an array ?. , how to create an array ?. and how to assign values to array elements?.Generally an array is group of variables holding values which have same data types. It means an array can store many values in them which are of same data types. Generally arrays are objects in Java, so they come under category of reference types. An array variable provides reference to an array object in a memory which can either hold primitives data types or reference data , based upon the declaration of an array. To retrieve elements of an array we use concepts of index.We provide the name of array variable and position number of array element which we want to retrieve in a square bracket. Let us go further and see how this whole concept works.


How to declare a One-Dimensional arrays ?.
A one-dimensional array is a linear list of elements of the same type. Let us see how to declare an One-Dimensional array:

Syntax : 
type array-name [ ]; 
Or 
type [ ] array-name;

Here, type declares the base type of the array and array-name is the name of the array.
Let us look into few of examples :

int [ ] intArray ; 
byte [ ] byteArray ;
Object [ ] objectArray;

In first example, we have declared an array which can hold int values. The array name is intArray. However, the array does not exist. The object intArray is set to null which represents an empty array.

How to create a One-Dimensional array ?.
In order to create a One-Dimensional array, we use new operator. The new operator creates an object of array in heap memory. Let us look into the syntax :

Syntax :
array-name = new type [ size ] ; 

Here array-name is the name of the array, type is the datatype of elements in the array and size is the number of elements.

Let us look into few of the examples :

intArray = new int [ 10 ] ;
byteArray = new byte [ 10 ];
objectArray = new Object [ 10 ] ;

In first example, we have created an integer array of 10 elements and assigns it to the variable intArray. After writing this syntax an array object is created in heap which can hold 10 integer values.


How to assign values to array through example ?.
Values can be assigned to an array by couple of ways. Let us look into syntax of some of those ways below :

Syntax :
array-name [ index ] = value ;

The value can be assign to each array element through their respective index number. As arrays are linear so they have sequential indexing. The indexing of an array starts with number 0.

Let us look into an example below :

for( int i = 0; i < 10 ; i++ )
intArray[ i ] = i + 1 ;

After for loop gets executed, values from 1 to 10 are assigned to array elements intArray[ 0 ] to intArray[ 9 ]. There is also an another way of assigning values to an array at the time of declaration. This assigning of value to an array is called as initialization. If we initialize an array, the size of an array need not be given. Let us look into syntax of initialization of an array.

syntax : 
type array-name [ ] = { value1, value2, value3 .... .... , valueN }
Let us look into the example:

int [ ] intArray = { 1,2,3,4,5,6,7,8,9,10} ;


Accessing Array Elements 

Generally, Arrays in Java are implemented as objects. Arrays in Java have a attribute called as length which stores the size of an array. Each and every array created has this attribute. It always store size of an array. This attribute or field can be accessed by using syntax :

array-name.length

Let us look into an example :

int [ ] intArray = { 4,5,6,7,8,9,1,2,3} ; 
for (int i = 0; i < intArray.length; i++ )
System.out.println( "Value of element" + i + " = " + intArray[ i ] );
Above code will print the values of an array element using length field. If we make an attempt to access array elements beyond the legal index, it throws an exception called ArrayIndexOutOfBoundsException.

 
© 2021 Learn Java by Examples Template by Hubberspot