Arrays:

Array is a data structure which will contain the data of same type (homogeneous).

Properties of arrays:

-> Arrays will be always stored in the heap only. -> It will have a "length" field which will contain the length of that array. ->Length field/variable is a constant. -> Arrays can contain fixed number of elements. Arrays
-> Array index will always start with zero. -> You can access max.length-1 index. -> In the above array, index = 6-1=5 and length=6 -> If you try to access less than zero or greater than max index then it will throw array index out of bound exception.

Declaring, allocating and initializing array:

-> int[ ] arr; // array declaration or int arr[ ]; //this is one more way of declaring array but it is not advisable. -> Declarations will not allocate any memory in the heap. -> To declare 10 int in arrays. int[10] arr; int arr[10]; -> int[ ] arr; //declaration arr = new int[10]; // allocation (or) arr = new int[ ] {1,2,3,4,5,6,7,8,9,10} allocation and initializing (or) int[ ] arr={1,2,3,4,5,6,7,8,9,10}; Arrays1
Arrays2
Assignment:

Take an array of size 10 and store 10 integer elements and print in the reverse order

     main()
        {
        int[ ] arr = {1,2,3....10};
        for(int i= arr.length-1; i>=0;i--)
        {
        System.out.println(arr[i]);
        }
}

Character array:

CharacterArray
CharacterArray1

Example:

         public class ArrayDemo
         {
           public static void main(String[ ] args)
           {
             // declaration & memory allocation
             int[ ] arr=new int[10];
             // Initialisation
            for(int i=0;i<arr.length;i++)
            {
              arr[i]=(int)(Math.random()*100);
            }
             //Display
             for(int i=0; i<arr.length;i++)
            {
              System.out.println(arr[i]);
            }
         }
      }

       class DataStructure
        {
         int[] arr; //declaration
           public DataStructure()
           {
             arr=new int[10];  //memory allocation
             for(int i=0; i<arr.length;i++)
              {
               arr[i]=(int) (Math.random()*100);  // initialization
               }
            }
            public void display()
            {
              for(int val:arr)
               { 
                System.out.println(val);
                }
             }
            public int find(int v)
            {
               for(int i=0;i<arr.length;i++)
                {
                 if(arr[i]==v)
                   {
                     return i;
                    }
                 }  
                  return -1;
             }
            }
Assignment: Assignment1
Assignment: Assignment2

More examples on arrays:

Example 1:

Take an integer array of 10 elements, insert 10 to 100 into that array using loop. Print all the elements of that array.

          public class IntegerArray
          {
	   public static void main(String[] args)  
           {
		  //declare array of 10 integers
		  int[] arr = new int[10];
		  //assign some values into array
                     for(int i=0; i < arr.length; i++)
                     {
			arr[i] = (i+1)*10;
		     }
		  System.out.println("----first way------");
		   //read the values and prints the values from the 
                   //array - by for loop
		     for(int i=0; i < arr.length; i++)
                     {
			System.out.println(arr[i]);
		     }
		  System.out.println("------second way-----");
		  //second way to read and print the values - by 
                  //enhanced for loop
		  for(int item: arr)
                  {
			System.out.println(item);
		   }
		   System.out.println("-----third way--------");
		   System.out.println(Arrays.toString(arr));
		
	       }
            }
Output for example 1: ----first way------ 10 20 30 40 50 60 70 80 90 100 ------second way----- 10 20 30 40 50 60 70 80 90 100 -----third way-------- [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] Example 2:

Take a String array of 3 elements hello, tech, palle. Print all the elements of that array.3times>

               public class StringArray  
                {

	           public static void main(String[] args)
                   {
		   String[] arr = new String[3];
		
		     arr[0] = "hello";
		     arr[1] = "tech";
		     arr[2] = "palle";
		
		    System.out.println("---first way---");
		    for(int i=0; i < arr.length; i++)
                      {
			System.out.println(arr[i]);
		       }
		    System.out.println("---second way---by for:each  loop");
		    for(String temp:arr)
                       {
			System.out.println(temp);
		        }
		       System.out.println("---third way---by Arrays class");
		       System.out.println(Arrays.toString(arr));
	          }
              }
}
Output for example 2: ---first way--- hello tech palle ---second way---by for:each loop hello tech palle ---third way---by Arrays class [hello, tech, palle] Example 3: Take an integer array of 10 elements and insert values through keyboard. Print all the elements of that array. Then add 1st 2nd element and put into 2nd element, add 1st, 2nd, 3rd element and insert into 3rd element and so on..
                 public class AddArrayElements
                 {
	        public static void main(String[] args)    
                {
		int[] arr = new int[10];
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter 10 int values 
                             into array");
		
		for(int i=0; i < arr.length; i++){
			//take value from keyboard
			int item = sc.nextInt();
			arr[i] = item;
		}
		System.out.println("-------values are--------");
		System.out.println(Arrays.toString(arr));
		
		for(int i=1; i < arr.length; i++){
			arr[i] = arr[i-1] + arr[i];
		}
		
		System.out.println("----after adding---");
		System.out.println(Arrays.toString(arr));
	}
}
Example 4:

Take an integer array of 10 elements and insert values through keyboard. Now ask user to enter a new number, and find out if that number is available in that array or not.

 
              public class SearchElement
              {

	        public static void main(String[] args)
                {
		  int[] arr = new int[10];//declared an array of 10 integers
		  Scanner sc = new Scanner(System.in);
		  System.out.println("Please enter 10 integers");
		  for(int i=0; i < arr.length; i++) 
                  {
		   	int item = sc.nextInt();
			arr[i] = item;
		   }
		   System.out.println("I am printing the array");
		   System.out.println(Arrays.toString(arr));
		
		   System.out.println("enter a number to search in the array");
		   int num = sc.nextInt();
		
		   System.out.println("we are searching for the  num");
		   for(int i=0; i < arr.length; i++)
                   {
			 if(num == arr[i])      
                         {
		         System.out.println("it is available at..."+i);
			}
  		   }
                }
              }
Example 5:

Take "techpalle" into a character array, and find out the location or index of 'a' in that array using a for loop

.
 
                     public class FindElementIndexInArray
                     {
                     public static void main(String[] args)
                       {
                           char[] carr =  {'t','e','c','h','p','a','l','l','e'};
	                   //find if 'a' is available in the array or not
	                   for(int i=0; i < carr.length; i++)
                           {
		             if('a' == carr[i])
                             {
		                System.out.println("its available at.."+i);
		             }
	                   }
	               }
                      }
Example 6:

find how many times 'e' is repeating in "techpalle".

             public class RepeatedElementInArray
              {
	        public static void main(String[] args)
                {
		  char[] arr =   {'t','e','c','h','p','a','l','l','e'};
		  int cnt = 0;
		  //take for loop to check 'e' with each element
		  for(int i=0; i < arr.length; i++)
                     {
	               if('e' == arr[i])
                         {
		               cnt++;
			 }
		      }
		   System.out.println("e is repeating.."+cnt);
	         }
              }
Example 7:

take values 10 to 100 in an array, and write a logic to calculate sum of all the elements in that array using for loop.

 
               public class SumOfAllElementsInArray
                {
	          public static void main(String[] args)
                   {
                   int[] arr = {10,20,30,40,50,60,70,80,90,100};
		   int sum = 0;
		   //find sum of all the elements
		   for(int i=0; i < arr.length; i++)
                    {
		     sum = sum + arr[i];
		     }
		   System.out.println("sum is.."+sum);
	        }
              }
Example 8:

take a character array with techpalle value, and print alternative characters starting from 0th index.

 
          public class ArrayAssignment5 
          {
	       public static void main(String[] args)
               {
		char[] arr = {'t','e','c','h','p','a','l','l','e'};
		for(int i=0; i < arr.length; i++)
                  {
			if(i%2 == 0)
                         {
				System.out.println(arr[i]);
			  }
		   }
	        }
             }
Example 9:

 
          public class ArrayReplace
            {
	      public static void main(String[] args)
                {
		    //how to use charecter arrays
		    char[] arr = {'p','a','l','l','e'','t','e','c','h'};
		    System.out.println(arr.length);
		    System.out.println(Arrays.toString(arr));  //replace vowels with 'z'
		    for(int i=0; i < arr.length; i++)    
                     {
			switch(arr[i]){
			case 'a':
			case 'e':
			case 'i':
			case 'o':
			case 'u':
				arr[i] = 'z';
			}
		      }
		  System.out.println("print after replacing");
		  System.out.println(Arrays.toString(arr));
	       }
           }
Example 10:

take an integer array of 10 elements and insert some random values into that array using Math.random() method. Find out how many even number of elements in that array.

              public class ArrayEvenNumbers
              {
	        public static void main(String[] args)   
                {
		   int[] arr = new int[10];
		   int count = 0;
		  //insert random values
		     for(int i=0; i < arr.length; i++)
                     {
		      arr[i] = (int) (Math.random()*100);
		      }
		  System.out.println(Arrays.toString(arr));
		  //find out total no of even numbers in the array
		   for(int i=0; i < arr.length; i++)
                      {
		           if(arr[i]%2 ==0)
                          {
		           count++;
			  }
		  }
		System.out.println("total no of even numbers.."+count);
	     }
Example 11:

take an integer array of 10 elements and insert some random values into that array, and reverse all the elements in the same array location.

 
                 public class ArrayReverse
                  {
                     public static void main(String[] args)   
                         {
                            int[] arr = new int[4];
                            for (int k = 0; k < arr.length; k++)
                            {
                             arr[k] = (int) (Math.random() * 100);
                              System.out.println(arr[k]);
                            }
                            System.out.print("----------");
                            System.out.println();
                            for (int i = 0; i < arr.length / 2; i++)
                            {
                                 int temp = arr[i];
                                 arr[i] = arr[arr.length - 1 - i];
                                 arr[arr.length - 1 - i] = temp;
                            }
                            for (int j = 0; j < arr.length; j++) 
                             {
                             System.out.println(arr[j]);
                             }
                         }
                  }
Example 12:

Write a logic to find the common elements in two integer arrays.

 
                 public class CommonElementsInArray
                 {
                   public static void main(String a[])
                  {
                    int[] arr1 = {4,7,3,9,2};
                    int[] arr2 = {3,2,12,9,40,32,4};
                     for(int i=0;i < arr1.length;i++)
                      {
                        for(int j=0;j < arr2.length;j++)
                            {
                            if(arr1[i]==arr2[j])
                              {
                               System.out.println(arr1[i]);
                              }
                            }
                       }
                    }
               }    
Example 13:

Write a logic to implement bubble sort.

 
            public class BubbleSort
            {
	       public static  void main(String[] args)
               {
		int[] arr={12,23,43,34,3,6,7,1,9,6};
                int n =arr.length;
		int temp = 0;
                  for(int i=0; i < n; i++)
                    {
		     for(int j=1; j < (n-i); j++)
                      {
                       if(arr[j-1] > arr[j])
                          {
			   //swap the elements!
			    temp = arr[j-1];
			    arr[j-1] = arr[j];
			    arr[j] = temp;
			  }
		       }
	           }
	           for(int k=0;k