Download an array

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Catuṣkoṭi wikipedia , lookup

Transcript
Problem Description:
How to sort an array and search an element inside it?
Solution:
Following example shows how to use sort () and binarySearch () method to accomplish the task.
The user defined method printArray () is used to display the output:
import java.util.Arrays;
public class MainClass {
public static void main(String args[]) throws Exception {
int array[] = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 };
Arrays.sort(array);
printArray("Sorted array", array);
int index = Arrays.binarySearch(array, 2);
System.out.println("Found 2 @ " + index);
}
private static void printArray(String message, int array[]) {
System.out.println(message
+ ": [length: " + array.length + "]");
for (int i = 0; i < array.length; i++) {
if(i != 0){
System.out.print(", ");
}
System.out.print(array[i]);
}
System.out.println();
}
}
Result:
The above code sample will produce the following result.
Sorted array: [length: 10]
-9, -7, -3, -2, 0, 2, 4, 5, 6, 8
Found 2 @ 5
Problem Description:
How to sort an array and insert an element inside it?
Solution:
Following example shows how to use sort () method and user defined method insertElement ()to
accomplish the task.
import java.util.Arrays;
public class MainClass {
public static void main(String args[]) throws Exception {
int array[] = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 };
Arrays.sort(array);
printArray("Sorted array", array);
int index = Arrays.binarySearch(array, 1);
System.out.println("Didn't find 1 @ "
+ index);
int newIndex = -index - 1;
array = insertElement(array, 1, newIndex);
printArray("With 1 added", array);
}
private static void printArray(String message, int array[]) {
System.out.println(message
+ ": [length: " + array.length + "]");
for (int i = 0; i < array.length; i++) {
if (i != 0){
System.out.print(", ");
}
System.out.print(array[i]);
}
System.out.println();
}
private static int[] insertElement(int original[],
int element, int index) {
int length = original.length;
int destination[] = new int[length + 1];
System.arraycopy(original, 0, destination, 0, index);
destination[index] = element;
System.arraycopy(original, index, destination, index
+ 1, length - index);
return destination;
}
}
Result:
The above code sample will produce the following result.
Sorted array: [length: 10]
-9, -7, -3, -2, 0, 2, 4, 5, 6, 8
Didn't find 1 @ -6
With 1 added: [length: 11]
-9, -7, -3, -2, 0, 1, 2, 4, 5, 6, 8
Problem Description:
How to determine the upper bound of a two dimentional array ?
Solution:
Following example helps to determine the upper bound of a two dimentional array with the use
of arrayname.length.
public class Main {
public static void main(String args[]) {
String[][] data = new String[2][5];
System.out.println("Dimension 1: " + data.length);
System.out.println("Dimension 2: " + data[0].length);
}
}
Result:
The above code sample will produce the following result.
Dimension 1: 2
Dimension 2: 5
Problem Description:
How to reverse an array list ?
Solution:
Following example reverses an array list by using Collections.reverse(ArrayList)method.
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
arrayList.add("D");
arrayList.add("E");
System.out.println("Before Reverse Order: " + arrayList);
Collections.reverse(arrayList);
System.out.println("After Reverse Order: " + arrayList);
}
}
Result:
The above code sample will produce the following result.
Before Reverse Order: [A, B, C, D, E]
After Reverse Order: [E, D, C, B, A]
Problem Description:
How to write an array of strings to the output console ?
Solution:
Following example demonstrates writing elements of an array to the output console through
looping.
public class Welcome {
public static void main(String[] args){
String[] greeting = new String[3];
greeting[0] = "This is the greeting";
greeting[1] = "for all the readers from";
greeting[2] = "Java Source .";
for (int i = 0; i < greeting.length; i++){
System.out.println(greeting[i]);
}
}
}
Result:
The above code sample will produce the following result.
This is the greeting
For all the readers From
Java source .
Problem Description:
How to write an array of strings to the output console ?
Solution:
Following example demonstrates writing elements of an array to the output console through
looping.
public class Welcome {
public static void main(String[] args){
String[] greeting = new String[3];
greeting[0] = "This is the greeting";
greeting[1] = "for all the readers from";
greeting[2] = "Java Source .";
for (int i = 0; i < greeting.length; i++){
System.out.println(greeting[i]);
}
}
}
Result:
The above code sample will produce the following result.
This is the greeting
For all the readers From
Java source .
Problem Description:
How to search the minimum and the maximum element in an array ?
Solution:
This example shows how to search the minimum and maximum element in an array by using
Collection.max() and Collection.min() methods of Collection class .
import java.util.Arrays;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
Integer[] numbers = { 8, 2, 7, 1, 4, 9, 5};
int min = (int) Collections.min(Arrays.asList(numbers));
int max = (int) Collections.max(Arrays.asList(numbers));
System.out.println("Min number: " + min);
System.out.println("Max number: " + max);
}
}
Result:
The above code sample will produce the following result.
Min number: 1
Max number: 9
Problem Description:
How to merge two arrays ?
Solution:
This example shows how to merge two arrays into a single array by the use of
list.Addall(array1.asList(array2) method of List class and Arrays.toString () method of Array
class.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String args[]) {
String a[] = { "A", "E", "I" };
String b[] = { "O", "U" };
List list = new ArrayList(Arrays.asList(a));
list.addAll(Arrays.asList(b));
Object[] c = list.toArray();
System.out.println(Arrays.toString(c));
}
}
Result:
The above code sample will produce the following result.
[A, E, I, O, U]
Problem Description:
How to fill (initialize at once) an array ?
Solution:
This example fill (initialize all the elements of the array in one short) an array by using
Array.fill(arrayname,value) method and Array.fill(arrayname ,starting index ,ending index
,value) method of Java Util class.
import java.util.*;
public class FillTest {
public static void main(String args[]) {
int array[] = new int[6];
Arrays.fill(array, 100);
for (int i=0, n=array.length; i < n; i++) {
System.out.println(array[i]);
}
System.out.println();
Arrays.fill(array, 3, 6, 50);
for (int i=0, n=array.length; i< n; i++) {
System.out.println(array[i]);
}
}
}
Result:
The above code sample will produce the following result.
100
100
100
100
100
100
100
100
100
50
50
50
Problem Description:
How to extend an array after initialisation?
Solution:
Following example shows how to extend an array after initialization by creating an new array.
public class Main {
public static void main(String[] args) {
String[] names = new String[] { "A", "B", "C" };
String[] extended = new String[5];
extended[3] = "D";
extended[4] = "E";
System.arraycopy(names, 0, extended, 0, names.length);
for (String str : extended){
System.out.println(str);
}
}
}
Result:
The above code sample will produce the following result.
A
B
C
D
E
Problem Description:
How to sort an array and search an element inside it?
Solution:
Following example shows how to use sort () and binarySearch () method to accomplish the task.
The user defined method printArray () is used to display the output:
import java.util.Arrays;
public class MainClass {
public static void main(String args[]) throws Exception {
int array[] = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 };
Arrays.sort(array);
printArray("Sorted array", array);
int index = Arrays.binarySearch(array, 2);
System.out.println("Found 2 @ " + index);
}
private static void printArray(String message, int array[]) {
System.out.println(message
+ ": [length: " + array.length + "]");
for (int i = 0; i < array.length; i++) {
if(i != 0){
System.out.print(", ");
}
System.out.print(array[i]);
}
System.out.println();
}
}
Result:
The above code sample will produce the following result.
Sorted array: [length: 10]
-9, -7, -3, -2, 0, 2, 4, 5, 6, 8
Found 2 @ 5
Problem Description:
How to remove an element of array?
Solution:
Following example shows how to remove an element from array.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList objArray = new ArrayList();
objArray.clear();
objArray.add(0,"0th element");
objArray.add(1,"1st element");
objArray.add(2,"2nd element");
System.out.println("Array before removing an
element"+objArray);
objArray.remove(1);
objArray.remove("0th element");
System.out.println("Array after removing an
element"+objArray);
}
}
Result:
The above code sample will produce the following result.
Array before removing an element[0th element,
1st element, 2nd element]
Array after removing an element[0th element]
Problem Description:
How to remove one array from another array?
Solution:
Following example uses Removeall method to remove one array from another.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList objArray = new ArrayList();
ArrayList objArray2 = new ArrayList();
objArray2.add(0,"common1");
objArray2.add(1,"common2");
objArray2.add(2,"notcommon");
objArray2.add(3,"notcommon1");
objArray.add(0,"common1");
objArray.add(1,"common2");
objArray.add(2,"notcommon2");
System.out.println("Array elements of array1" +objArray);
System.out.println("Array elements of array2" +objArray2);
objArray.removeAll(objArray2);
System.out.println("Array1 after removing
array2 from array1"+objArray);
}
}
Result:
The above code sample will produce the following result.
Array elements of array1[common1, common2, notcommon2]
Array elements of array2[common1, common2, notcommon,
notcommon1]
Array1 after removing array2 from array1[notcommon2]
Problem Description:
How to find common elements from arrays?
Solution:
Following example shows how to find common elements from two arrays and store them in an
array.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList objArray = new ArrayList();
ArrayList objArray2 = new ArrayList();
objArray2.add(0,"common1");
objArray2.add(1,"common2");
objArray2.add(2,"notcommon");
objArray2.add(3,"notcommon1");
objArray.add(0,"common1");
objArray.add(1,"common2");
objArray.add(2,"notcommon2");
System.out.println("Array elements of array1"+objArray);
System.out.println("Array elements of array2"+objArray2);
objArray.retainAll(objArray2);
System.out.println("Array1 after retaining common
elements of array2 & array1"+objArray);
}
}
Result:
The above code sample will produce the following result.
Array elements of array1[common1, common2, notcommon2]
Array elements of array2[common1, common2, notcommon,
notcommon1]
Array1 after retaining common elements of array2 & array1
[common1, common2]
Problem Description:
How to find an object or a string in an Array?
Solution:
Following example uses Contains method to search a String in the Array.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList objArray = new ArrayList();
ArrayList objArray2 = new ArrayList();
objArray2.add(0,"common1");
objArray2.add(1,"common2");
objArray2.add(2,"notcommon");
objArray2.add(3,"notcommon1");
objArray.add(0,"common1");
objArray.add(1,"common2");
System.out.println("Array elements of array1"+objArray);
System.out.println("Array elements of array2"+objArray2);
System.out.println("Array 1 contains String common2?? "
+objArray.contains("common1"));
System.out.println("Array 2 contains Array1?? "
+objArray2.contains(objArray) );
}
}
Result:
The above code sample will produce the following result.
Array
Array
Array
Array
elements of array1[common1, common2]
elements of array2[common1, common2, notcommon, notcommon1]
1 contains String common2?? true
2 contains Array1?? false
Problem Description:
How to check if two arrays are equal or not?
Solution:
Following example shows how to use equals () method of Arrays to check if two arrays are equal
or not.
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws Exception {
int[] ary = {1,2,3,4,5,6};
int[] ary1 = {1,2,3,4,5,6};
int[] ary2 = {1,2,3,4};
System.out.println("Is array 1 equal to array 2?? "
+Arrays.equals(ary, ary1));
System.out.println("Is array 1 equal to array 3?? "
+Arrays.equals(ary, ary2));
}
}
Result:
The above code sample will produce the following result.
Is array 1 equal to array 2?? true
Is array 1 equal to array 3?? false
Problem Description:
How to compare two arrays?
Solution:
Following example uses equals method to check whether two arrays are or not.
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws Exception {
int[] ary = {1,2,3,4,5,6};
int[] ary1 = {1,2,3,4,5,6};
int[] ary2 = {1,2,3,4};
System.out.println("Is array 1 equal to array 2?? "
+Arrays.equals(ary, ary1));
System.out.println("Is array 1 equal to array 3?? "
+Arrays.equals(ary, ary2));
}
}
Result:
The above code sample will produce the following result.
Is array 1 equal to array 2?? true
Is array 1 equal to array 3?? false