Data Structure

Data structure is way in which we store data and the relationship among them and the functions or operations that can be applied to. In this tutorial, we will see each data structure one by one. Will solve few problems for the same.

Array

Array can be accessed with index based mechanism, where 0 pointing to first element and n-1 pointing to the last element, n being the size of array.

arrayBasic

Lets call this array as "myfirst", so myfirst of 0 will be 10, myfirst of 1 will be 11. Where 0 and 1 are index positions.

Time Complexity

Array Time complexity

Now lets see how to create an array in java and python and access it's element.

 
package com.gauraw.learning.array;
 
/**
 * Example shows how to create an array and access its element randomly
 * @author gaurawkumarsingh
 *
 */
public class Example1 {
 
    public static void main(String ...ars) {
 
        // one way of declaring 
        int [] myfirst = { 10, 11, 12, 13, 14, 15, 16, 17, 18};
        System.out.println( "Value at first position ->"+myfirst[0]);
 
        // another way of creating array 
        int []test1 = new int[10];
 
        for ( int i =0 ; i < test1.length ;i++) {
            test1[i] = i;
        }
 
        System.out.println("The second error ");
        for ( int i=0 ;< test1.length ;i++) {
            System.out.println(test1[i]);
        }
 
    }
}

Lets see now multi dimensional array

 
package com.gauraw.learning.array;
 
/**
 * Class help in  learning multi diemensinal array
 * @author gaurawkumarsingh
 *
 */
public class Example2 {
 
    public static void main(String ...strings ) {
 
        /**
         * Create an array of 2*3 where number of rows is 2 and number of columns is 3
         */
 
        int [][] multi = new int[2][3];
 
        /**
         * Insert element into multi dimensinal array
         */
 
        for ( int i =0 ;< multi.length ; i++ )
        {
            for ( int j =0 ; j< multi[i].length ; j++) {
                multi[i][j]= i+j;
            }
        }
 
        /**
         * Print multi diemensinal array
         * Note how we are iterating the columns
         */
 
        for ( int i =0 ;< multi.length; i++) {
            for ( int j =0 ; j < multi[i].length;j++) {
                System.out.print(multi[i][j]+" ");
            }
            System.out.println();
        }
    }
 
}

Read ( Any Where)

Array can be accessed based on indexes, time complexity is O(1)

package com.gauraw.learning.array;
 
/**
 * Access Element based on index
 * @author gaurawkumarsingh
 *
 */
public class Example3 {
 
    public static void main(String ...strings) {
 
        /**
         * Create the array
         */
 
        int array[] = {1,2,3,4,5};
 
        //Accessing the first element in array 
        System.out.println(array[1]);
 
    }
}

Add / Remove ( At the end )