Array

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

Single Dimensional Array in JAVA:

1
class arrdemo { public static void main(String[] args) { int marks[]; // array declaration that array would be of integer type marks =new int[5]; // new operator will allocates memory to the array int tot=0; for (int i=0;i<=4 ;i++ ) { marks[i] = i; // storing values in array elements } for (int i=0;i<=4 ;i++ ) // printing value of array elements. { System.out.println(marks[i]); } for (int i=0;i<=4;i++ ) { tot = marks[i] + tot; // adding array elements } System.out.println("Addition of array elements is="+tot); for (int i=4;i>=0 ;i-- ) { System.out.println(marks[i]); // printing value of array elements in reverse order } } }

2
// Average an array of values. fclass Average { public static void main(String args[]) { double nums[] = {10.1, 11.2, 12.3, 13.4, 14.5}; double result = 0; int i; for(i=0; i<5; i++) result = result + nums[i]; System.out.println("Average is " + result / 5); } }

3
// Demonstrate a one-dimensional array. class Array { public static void main(String args[]) { int month_days[]; month_days = new int[12]; month_days[0] = 31; month_days[1] = 28; month_days[2] = 31; month_days[3] = 30; month_days[4] = 31; month_days[5] = 30; month_days[6] = 31; month_days[7] = 31; month_days[8] = 30; month_days[9] = 31; month_days[10] = 30; month_days[11] = 31; System.out.println("April has " + month_days[3] + " days."); } }

4
// An improved version of the previous program. class AutoArray { public static void main(String args[]) { int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; System.out.println("April has " + month_days[3] + " days."); } }

5
One-Dimensional Arrays 1. Allocation a. Fixed-Size Array
b. import java.util.Scanner; c. class MyCode { d. public static void main(String[] args) { e. Scanner scanner = new Scanner(System.in);

int[] a = new int[5]; System.out.print("Enter 5 integers: "); for (int i=0; i<5; i++) a[i] = scanner.nextInt(); System.out.println("Here are the 5 integers in reverse:"); for (int i=4; i>=0; i--) System.out.println(a[i]); } }

f. Variable-Sized Array
g. import java.util.Scanner; h. class MyCode { i. public static void main(String[] args) { j. Scanner scanner = new Scanner(System.in); k. System.out.print("Enter # of integers: "); int n = scanner.nextInt(); int[] a = new int[n]; System.out.print("Enter " + n + " integers: "); for (int i=0; i<n; i++) a[i] = scanner.nextInt(); System.out.println("Here are the " + n + " integers in reverse:"); for (int i=n-1; i>=0; i--) System.out.println(a[i]); } }

l. Statically-Allocated Array
m. class MyCode { n. public static void main(String[] args) { o. int[] a = { 5, 2, 1, 3, 6, 4 }; int n = a.length; System.out.println("Here are the " + n + " integers in reverse:"); for (int i=n-1; i>=0; i--) System.out.println(a[i]); } }

2. Default Values
3. import java.util.Scanner; 4. class MyCode { 5. public static void main(String[] args) { 6. int[] ai = new int[1]; 7. double[] ad = new double[1]; 8. boolean[] ab = new boolean[1]; 9. char[] ac = new char[1]; String[] as = new String[1]; System.out.println("Array default values for:"); System.out.println(" int: " + ai[0]); System.out.println(" double: " + ad[0]); System.out.println(" boolean: " + ab[0]); System.out.println(" char: (Unicode " + (int)ac[0] + ")"); System.out.println(" String: " + as[0]); } }

10. Array Parameters


11. class MyCode { 12. public static void printInReverse(int[] a) { 13. int n = a.length; 14. System.out.println("Here are the " + n + " integers in reverse:"); 15. for (int i=n-1; i>=0; i--) 16. System.out.println(a[i]); } public static void main(String[] args) { int[] a = { 5, 2, 1, 3, 6, 4 }; printInReverse(a); } }

17. Array Return Types


18. import java.util.*; 19. class MyCode { public static final Random random = new Random(); public static int[] getArrayOfRandoms(int n) { int[] a = new int[n]; for (int i=0; i<n; i++) a[i] = random.nextInt(100); return a; } public static void main(String[] args) { int[] a = getArrayOfRandoms(10); System.out.println("Here are the randoms: "); for (int i=0; i<a.length; i++) System.out.println(a[i]); } }

20. Traversal a. Forward Traversal


b. class MyCode { c. public static void main(String[] args) { d. int[] a = { 3, 2, 4, 1 }; e. System.out.println("Forward Traversal:"); f. for (int i=0; i<a.length; i++) g. System.out.println(a[i]); h. } }

i. Backward Traversal
j. class MyCode { k. public static void main(String[] args) { l. int[] a = { 3, 2, 4, 1 }; m. System.out.println("Backward Traversal:"); n. for (int i=a.length-1; i>=0; i--) o. System.out.println(a[i]); p. } }

q. Alternative Backward Traversal


r. class MyCode {

s. t. u. v. w. x. }

public static void main(String[] args) { int[] a = { 3, 2, 4, 1 }; System.out.println("Alternative Backward Traversal:"); for (int i=0; i<a.length; i++) System.out.println(a[(a.length-1)-i]); }

21. Arrays Methods a. equals


b. import java.util.Arrays; c. class MyCode { d. public static void main(String[] args) { e. int[] a = { 3, 4, 2, 1 }; f. int[] b = { 3, 4, 2, 1 }; g. int[] c = { 3, 4, 2 }; h. System.out.println(Arrays.equals(a, b)); i. System.out.println(Arrays.equals(a, c)); j. } }

k. toString
l. import java.util.Arrays; m. class MyCode { n. public static void main(String[] args) { o. int[] a = { 3, 4, 2, 1 }; p. System.out.println(a); // surprised? q. System.out.println(Arrays.toString(a)); r. } }

s. fill
t. import java.util.Arrays; u. class MyCode { v. public static void main(String[] args) { w. int[] a = { 3, 4, 2, 1 }; x. System.out.println(Arrays.toString(a)); y. Arrays.fill(a, 9); z. System.out.println(Arrays.toString(a)); aa. } }

bb. sort
cc. dd. ee. ff. gg. hh. ii. jj. } import java.util.Arrays; class MyCode { public static void main(String[] args) { int[] a = { 3, 4, 2, 1 }; System.out.println(Arrays.toString(a)); Arrays.sort(a); System.out.println(Arrays.toString(a)); }

kk. Online Arrays API http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html 22. Swap a. Broken Swap


b. import java.util.Arrays; c. class MyCode { d. public static void brokenSwap(int x, int y) { e. int temp = x; f. x = y; g. y = temp; h. } i. public static void main(String[] args) { j. int[] a = { 3, 4, 2, 1 }; k. System.out.println(Arrays.toString(a)); l. brokenSwap(a[0], a[1]); m. System.out.println(Arrays.toString(a)); n. } }

o. Working Swap
p. import java.util.Arrays; q. class MyCode { r. public static void swap(int[] a, int i, int j) { s. int temp = a[i]; t. a[i] = a[j]; u. a[j] = temp; v. } w. public static void main(String[] args) { x. int[] a = { 3, 4, 2, 1 }; y. System.out.println(Arrays.toString(a)); z. swap(a,0,1); aa. System.out.println(Arrays.toString(a)); bb. } }

23. Copy a. Broken Copy


b. import java.util.Arrays; c. class MyCode { d. public static void main(String[] args) { e. int[] a = { 1, 2, 3}; int[] copy = a; // BROKEN!!! Not a COPY of a! // After we copy the array, we change the original // and the copy, but what happens...? a[0] = 99; copy[1] = -99; System.out.println(Arrays.toString(a)); System.out.println(Arrays.toString(copy)); } }

f. Working Copy

g. import java.util.Arrays; h. class MyCode { i. public static int[] copy(int[] a) { j. int[] copy = new int[a.length]; k. for (int i=0; i<a.length; i++) l. copy[i] = a[i]; m. return copy; } public static void main(String[] args) { int[] a = { 1, 2, 3}; int[] copy = copy(a); // After we copy the array, we change the original // and the copy, but what happens...? a[0] = 99; copy[1] = -99; System.out.println(Arrays.toString(a)); System.out.println(Arrays.toString(copy)); } }

24. Equals a. Broken Equals


b. class MyCode { c. public static void main(String[] args) { d. int[] a = { 1, 2, 3}; e. int[] b = { 1, 2, 3}; f. System.out.println(a == b); // surprised? g. } }

Working Equals
import java.util.Arrays; class MyCode { public static void main(String[] args) { int[] a = { 1, 2, 3}; int[] b = { 1, 2, 3}; System.out.println(Arrays.equals(a,b)); } }

25. Insertion
26. import java.util.Arrays; 27. class MyCode { 28. public static int[] add(int[] a, int newValue) { 29. int[] result = new int[a.length+1]; 30. for (int i=0; i<a.length; i++) 31. result[i] = a[i]; 32. result[a.length] = newValue; 33. return result; } public static void main(String[] args) { int[] a = new int[0]; System.out.println(Arrays.toString(a)); a = add(a, 3); System.out.println(Arrays.toString(a)); a = add(a, 5);

System.out.println(Arrays.toString(a)); } }

34. Deletion
35. import java.util.Arrays; 36. class MyCode { 37. 38. // Removes every occurrence of value, if any, in the array "a" 39. public static int[] remove(int[] a, int value) { 40. int[] result = new int[a.length - count(a,value)]; 41. int resultI = 0; 42. for (int i=0; i<a.length; i++) 43. if (a[i] != value) { 44. result[resultI] = a[i]; 45. resultI++; 46. } 47. return result; } // Helper method for remove. Returns the number of times that // value occurs in the array a. public static int count(int[] a, int value) { int count = 0; for (int i=0; i<a.length; i++) if (a[i] == value) count++; return count; } public static void main(String[] args) { int[] a = { 2, 3, 5, 3, 1, 4 }; System.out.println(Arrays.toString(a)); a = remove(a, 1); System.out.println(Arrays.toString(a)); a = remove(a, 3); System.out.println(Arrays.toString(a)); } }

48. Examples a. The Locker Problem


b. import java.util.*; c. class MyCode { d. public static void main(String[] args) { e. // 1. Read in # of lockers f. Scanner scanner = new Scanner(System.in); g. System.out.print("How many lockers: "); h. int lockers = scanner.nextInt(); i. j. // 2. Allocate array of lockers, all closed at first k. // (We will ignore locker[0].) l. boolean[] isOpen = new boolean[lockers+1]; m. n. // 3. Student N visits every Nth locker o. int student, locker; p. for (student=1; student<=lockers; student++) { q. for (locker=student; locker<=lockers; locker += student) r. isOpen[locker] = !isOpen[locker];

s. t. u. v. w. x. y. z. }

} // 4. Print out open lockers System.out.println("Open lockers:"); for (locker=1; locker<=lockers; locker++) if (isOpen[locker]) System.out.println(locker); }

aa. Anagrams
bb. import java.util.*; class MyCode { // Returns an array of size 26 containing the counts // of each of the 26 letters in the given string. public static int[] charCounts(String s) { s = s.toUpperCase(); int[] counts = new int[26]; for (int i=0; i<s.length(); i++) { char c = s.charAt(i); if ((c >= 'A') && (c <= 'Z')) ++counts[(c - 'A')]; } return counts; } // Two strings are anagrams if the all the counts // of each of the 26 letters in each string are the same. public static boolean isAnagram(String s1, String s2) { int[] counts1 = charCounts(s1); int[] counts2 = charCounts(s2); return (Arrays.equals(counts1, counts2)); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter two strings: "); String s1 = scanner.next(); String s2 = scanner.next(); System.out.println("isAnagram(" + s1 + "," + s2 + ") = " + isAnagram(s1,s2)); } }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy