• No results found

Arrays

N/A
N/A
Protected

Academic year: 2020

Share "Arrays"

Copied!
9
0
0

Loading.... (view fulltext now)

Full text

(1)

Page 1 of 9

Arrays:

An array

is a structure that holds multiple values of the

same type. The length of an array is established when

the array is created. After creation, an array is a

fixed-length structure.

An array element

is one of the values within an array

and is accessed by its position within the array.

(2)

Page 2 of 9

Creating and Using Arrays (another example)

public class ArrayDemo {

public static void main(String[] args) {

int[] anArray; // declare an array of integers

anArray = new int[10]; // create an array of integers // assign a value to each array element and print for (int i = 0; i < anArray.length; i++) {

anArray[i] = i;

System.out.print(anArray[i] + " "); }

System.out.println(); } //end main

} //end class

Declaring a Variable to Refer to an Array

int[] anArray;

float[] anArrayOfFloats;

(3)

Page 3 of 9

Creating an Array

new elementType[arraySize]

anArray = new int[10];

// create an array of

integers

Array Initializers

boolean[] answers = { true, false, true,

true, false };

Accessing Array Elements

for (int i = 0; i < anArray.length; i++) {

anArray[i]

= i;

System.out.print(

anArray[i]

+ " ");

}

Getting the Size of an Array

arrayName.length

(4)

Page 4 of 9

Arrays of Objects

public class ArrayOfStringsDemo {

public static void main(String[] args) {

String[] anArray = {

"String One",

"String Two",

"String Three"};

for (int i = 0; i<anArray.length; i++) {

System.out.println(anArray[i].toLowerCase());

}

}

}

Arrays of Arrays (Multi-dimensional arrays)

Check the

MultiDimArrayDemo

example in the

tutorial.

public class ArrayOfArraysDemo {

public static void main(String[] args) {

String[][] countries = {

{ "North America", “USA", "Mexico", "Canada”},

{ "South America","Brazil", "Argentina", Uruguey"}, { "Europe", "England", "Germany", "Netharland", "Spain", "Itally"}

};

for (int i = 0; i < countries.length; i++) { System.out.print(countries [i][0] + ": ");

for (int j = 1; j < countries [i].length; j++) { System.out.print(countries [i][j] + " "); }

System.out.println(); }

(5)

Page 5 of 9

Another example of Arrays of Arrays

public class ArrayOfArraysDemo2 {

public static void main(String[] args) {

int[][] aMatrix = new int[4][];

//populate matrix

for (int i = 0; i < aMatrix.length; i++) {

aMatrix[i] = new int[i+1]; //create sub-array for (int j = 0; j < aMatrix[i].length; j++) {

aMatrix[i][j] = i + j; }

}

//print matrix

for (int i = 0; i < aMatrix.length; i++) {

for (int j = 0; j < aMatrix[i].length; j++) { System.out.print(aMatrix[i][j] + " "); }

System.out.println(); }

(6)

Page 6 of 9

Stack example

/**

* @author Nagesh Chauhan (slightly edited) */

public class StackDemo {

static final int capacity = 3; int arr[] = new int[capacity];

int top = -1;

public void push(int pushedElement) { if (top < capacity - 1) {

top++; //top = top+1

arr[top] = pushedElement;

System.out.println("Element " + pushedElement + " is pushed to Stack !");

printElements(); } else {

System.out.println("Stack Overflow !"); }

}

public void pop() { if (top >= 0) {

top--; //top = top-1

System.out.println("Pop operation done !"); } else {

System.out.println("Stack Underflow !"); }

}

public void printElements() { if (top >= 0) {

System.out.println("Elements in stack :"); for (int i = 0; i <= top; i++) {

System.out.println(arr[i]); }

(7)

Page 7 of 9

public static void main(String[] args) { StackDemo stackDemo = new StackDemo();

stackDemo.pop(); stackDemo.push(23); stackDemo.push(2); stackDemo.push(73); stackDemo.push(21); stackDemo.pop(); stackDemo.pop(); stackDemo.pop(); stackDemo.pop(); }

}

OUTPUT:

Stack Underflow !

Element 23 is pushed to Stack ! Elements in stack :

23

Element 2 is pushed to Stack ! Elements in stack :

23 2

Element 73 is pushed to Stack ! Elements in stack :

23 2 73

(8)

Page 8 of 9

Copying Arrays

public static void

arraycopy(Object source, int srcIndex, Object dest, int destIndex, int length)

Example:

class ArrayCopyDemo {

public static void main(String[] args) {

char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e', 'i', 'n', 'a', 't', 'e', 'd' };

char[] copyTo = new char[7];

System.arraycopy(copyFrom, 2, copyTo, 0, 7);

System.out.println(new String(copyTo)); }

(9)

Page 9 of 9

Array manipulations

Java SE provides several methods for performing array

manipulations (common tasks, such as copying, sorting and

searching arrays) in the

java.util.Arrays

class.

class ArrayCopyOfDemo {

public static void main(String[] args) {

char[] copyFrom = {'d', 'e', 'c', 'a', 'f', 'f', 'e', 'i', 'n', 'a', 't', 'e', 'd'};

char[] copyTo = java.util.Arrays.copyOfRange(copyFrom, 2, 9);

System.out.println(new String(copyTo)); }

}

Some other useful operations provided by methods in

the

java.util.Arrays

class, are:

Searching an array for a specific value to get the index at

which it is placed (the binarySearch method).

Comparing two arrays to determine if they are equal or

not (the

equals

method).

Filling an array to place a specific value at each index

(the

fill

method).

Sorting an array into ascending order. This can be done

either sequentially, using the sort method, or

concurrently, using the

parallelSort

method

References

Related documents

(2007) Peptide arrays on cellulose support: SPOT synthesis – a time and cost efficient method for synthesis of large num- bers of peptides in a parallel and addressable fashion..

That is more than 1000 times faster than sequential Dijkstra’s algorithm, 67 times faster than sequential PHAST, and 6.7 times faster than computing 64 trees on the CPU in parallel

architectures ranging from pipelined structures, array processors and multiprocessor systems.. Dedicated high-performance architectures and systems, as

2 Since arrays are always passed by reference, all changes made to the array elements inside the function will be made to the original array.. The only way to protect the elements

maximizing the continuous second-order difference co-array lags, such as the co-prime arrays and nested arrays, we can shift the second sparse array so that its first sensor will

■ cannot store them in the same array ■ so use two different arrays &#34;side

At Demartek, we are even hearing that some large IT shops are including all-flash array storage in their future purchase plans, and that all-flash arrays are becoming their

Write a method called allLess that accepts two arrays of integers and returns true if each element in the first array is less than the element at the same index in the second