Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Traversing

public class New { Element at index 0: 100


Element at index 1: 200
public static void main(String[] args) { Element at index 2: 300
// declares an array of integers Element at index 3: 400
int[] anArray; Element at index 4: 500
Element at index 5: 600
// allocates memory for 10 integers Element at index 6: 700
Element at index 7: 800
anArray = new int[10];
Element at index 8: 900
Element at index 9: 1000
// initialize first element
anArray[0] = 100;
// initialize second element
anArray[1] = 200;
// and so forth
anArray[2] = 300;
anArray[3] = 400;
anArray[4] = 500;
anArray[5] = 600;
anArray[6] = 700;
anArray[7] = 800;
anArray[8] = 900;
anArray[9] = 1000;

System.out.println("Element at index 0:
"
+ anArray[0]);
System.out.println("Element at index 1:
"
+ anArray[1]);
System.out.println("Element at index 2:
"
+ anArray[2]);
System.out.println("Element at index 3:
"
+ anArray[3]);
System.out.println("Element at index 4:
"
+ anArray[4]);
System.out.println("Element at index 5:
"
+ anArray[5]);
System.out.println("Element at index 6:
"
+ anArray[6]);
System.out.println("Element at index 7:
"
+ anArray[7]);
System.out.println("Element at index 8:
"
+ anArray[8]);
System.out.println("Element at index 9:
"
+ anArray[9]);
}

} // end of class Interest

Sorting

Bubble Sort:
The technique we use is called “Bubble Sort” because the bigger
value gradually bubbles their way up to the top of array like air
bubble rising in water, while the
small values sink to the bottom of array.

This technique is to make several passes through the array. On


each pass,successive pairs of elements are compared. If a pair is
in increasing order (or the
values are identical), we leave the values as they are. If a pair is in
decreasing order, their values are swapped in the array.
package Name; The original order:
0: Lauren 1: Audrina 2:
import java.util.Arrays; Heidi 3: Whitney 4:
Stephanie 5: Spencer 6:
public class Name { Lisa 7: Brody 8: Frankie
9: Holly 10: Jordan 11:
public static void main(String[] args) { Brian 12: Jason
String names[] = { "Lauren", "Audrina", The new order:
"Heidi", "Whitney", 0: Audrina 1: Brian 2:
"Stephanie", "Spencer", "Lisa", Brody 3: Frankie 4: Heidi
"Brody", "Frankie", 5: Holly 6: Jason 7:
"Holly", "Jordan", "Brian", "Jason" }; Jordan 8: Lauren 9: Lisa
System.out.println("The original order:"); 10: Spencer 11: Stephanie
for (int i = 0; i < names.length; i++) { 12: Whitney
System.out.print(i + ": " + names[i] +
" ");
}
Arrays.sort(names);
System.out.println("\nThe new order:");
for (int i = 0; i < names.length; i++) {
System.out.print(i + ": " + names[i] +
" ");
}
System.out.println();
}

Deletion

Deleting any element from an array is an O(N) operation. You can do the
following.
1. Initialize i = 0. count = 0.
2. Iterate through array1[] and search for element delete[i].
3. If you encounter an element array1[j] > delete[i], this means that
delete[i] does not exist in array[].
Increment i to check for next element in delete array.
4. If you find an element array1[j] == delete[i], then increment count. and
increment i.
5. Keep copying array1[j] to array1[j - count].
array1[j - count] = array1[j];
6. Continue till the end of array1. At the end, resize array1 to be of size
size - count.

import java.util.Arrays; Output:


import org.apache.commons.lang.ArrayUtils; Original Array : size : 5
Contents : [101, 102,
/** 103, 104, 105]
 * Size of array after
 * Java program to show how to remove element from removing an element  : 4
Array in Java Content of Array after
 * This program shows How to use Apache Commons removing an object :
ArrayUtils to delete [101, 102, 104, 105]
 * elements from primitive array.
 *
 * @author http://java67.blogspot.com
 */
public class RemoveObjectFromArray{
    public static void main(String args[]) {
                 
        //let's create an array for demonstration
purpose
        int[] test = new int[] { 101, 102, 103, 104,
105};
     
        System.out.println("Original Array : size : "
+ test.length );
        System.out.println("Contents : " +
Arrays.toString(test));
     
        //let's remove or delete an element from
Array using Apache Commons ArrayUtils
        test = ArrayUtils.remove(test, 2); //removing
element at index 2
     
        //Size of array must be 1 less than original
array after deleting an element
        System.out.println("Size of array after
removing an element  : " + test.length);
        System.out.println("Content of Array after
removing an object : "
+ Arrays.toString(test));
     
    }
 
}
Searching

Or

package test; Output


Does programming Array
import java.util.Arrays; contains Java? true
import java.util.List; Index of Java in
import org.apache.commons.lang.ArrayUtils; programming Array is : 0
Does programming array
/** has Java? true
 * What is index of Java in
 * Java program to check if an Array contains an Item array? 0
or not
 * and finding index of that item. For example How to
check if
 * an String array contains a particular String or
not and What is
 * index of that String in Java program.
 *
 * @author http://java67.blogspot.com
 */
public class ArrayTest{

 
    public static void main(String args[]) {
 
      String[] programming = new String[]{"Java", "C+
+", "Perl", "Lisp"};
   
      // Checking an String in String Array by
converting Array To ArrayList
      List<String> programmingList =
Arrays.asList(programming);
   
      //Checking does this Array contains Java

Read more: http://java67.blogspot.com/2012/10/how-to-


find-index-of-item-in-array-in.html#ixzz42PDFG6l9
boolean result = programmingList.contains("Java");
   
      System.out.println("Does programming Array
contains Java? " + result);
   
      int index = programmingList.indexOf("Java");
   
      System.out.println("Index of Java in
programming Array is : " + index);
   
   
      // Checking item in Array using Apache Commons
ArrayUtils class
   
      System.out.println("Does programming array has
Java? " +

ArrayUtils.contains(programming, "Java"));
      System.err.println("What is index of Java in
array? " +

ArrayUtils.indexOf(programming, "Java"));    
   
    }
     
}

You might also like