Array in java is a group of like-typed variables referred to by a common name. Arrays in Java work differently than they do in C/C++. Following are some important points about Java arrays.

In Java, all arrays are dynamically allocated. (discussed below) Arrays are stored in contagious memory [consecutive memory locations]. Since arrays are objects in Java, we can find their length using the object property length. This is different from C/C++, where we find length using sizeof. A Java array variable can also be declared like other variables with [] after the data type. The variables in the array are ordered, and each has an index beginning from 0. Java array can also be used as a static field, a local variable, or a method parameter. The size of an array must be specified by int or short value and not long. The direct superclass of an array type is Object. Every array type implements the interfaces Cloneable and java.io.Serializable. This storage of arrays helps us randomly accessing the elements of an array [Support Random Access]. The size of the array cannot be altered(once initialized). However, an array reference can be made to point to another array. An array can contain primitives (int, char, etc.) and object (or non-primitive) references of a class depending on the definition of the array. In the case of primitive data types, the actual values are stored in contiguous memory locations.

dataType[] arrayName;

For example, if you wanted to create an array that would hold integer values, you could declare a variable called numbers like this:

int[] numbers;

Once you have declared the array, you need to allocate memory for it using the new keyword, like this:

numbers = new int[size];

Here, size is an integer value that specifies the number of elements that the array will hold. For example, if you wanted to create an array that could hold 10 integer values, you would write:

numbers = new int[10];

Hack 1 Swap the first and last element in the array

import java.util.Arrays;
import java.util.*;

 public class ArrayMethods {
 public static void main(String[] args)
 {
     
	swapping();
    
 }

static void swapping() {
    int[] array = {20, 30, 40};
	System.out.println("Original Array: "+Arrays.toString(array));
    int x = array[0];
	array[0] = array[array.length-1];
	array[array.length-1] = x;
	System.out.println("New array after swapping the first and last elements: "+Arrays.toString(array));  
  }





  
 
}

ArrayMethods.main(null)
Original Array: [20, 30, 40]
New array after swapping the first and last elements: [40, 30, 20]

Hack to Evens in array

import java.util.Arrays;
import java.util.*;

 public class ArrayMethods {
 public static void main(String[] args)
 {
     
	
    evens();
 }







  
   static void evens(){
 int[] number = {6, 8, 11, 16, 21, 23};
	System.out.println("Original Array: "+Arrays.toString(number)); 
    
   for(int i=0;i<number.length;i++){  
      if(number[i]%2==0){  
        System.out.println(number[i]);  
        }  

  } 
}

ArrayMethods.main(null)

Hack 3 check order (extra credit)

public class ArrayMethods {

    public static boolean isSorted(int[] a) 
    {
        int i;
        for(i = 0; i < a.length; i ++);{
            if (a[i] < a[i+1]) {
                return true;
            } else {
                return false;   
            }
        }
    }
    public static void main(String[] args)
    {
        int ar[] = {3,5,6,7};
        System.out.println(isSorted(ar));   
    }


    
}

Hack 4 checking duplicates in array (extra credit)

public class ArrayMethods{

    
    public static void main(String[] args)
    {
        
       
       duplicate();
    }
    
    
  
  
    public static duplicates {


        int[] number = {6, 8, 11, 16, 21, 23};
	System.out.println("Original Array: "+Arrays.toString(number)); 
        
        for (int i = 0; i < names.length; i++) {
        for (int j = i + 1 ; j < names.length; j++) {
             if (names[i].equals(names[j])) {
                      // got the duplicate element
             }
        }
    }

}




    
}

week 6 arrays