1 / 10

04 Arrays

Learn about arrays, including how to declare, construct, and initialize them. Understand how to manipulate arrays using methods.

starlar
Download Presentation

04 Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 04 Arrays

  2. Outline • What is an array? • Creating arrays • Declaration • Construction • Initialization • Using arrays

  3. Objectives • Define array • Understand different ways to declare an array • Understand different ways to create an array • Understand different ways to initialize arrays • Utilize methods for manipulating arrays

  4. An array is simply a sequence of either objects or primitives, all of the same type and packaged together under one identifier name. type 0 0 0 1 0 2 3 0 Identifier name Indexing operator What is an Array? A one-dimensional array on the heap Values The heap int[] numbers; int [] numbers object Indices int numbers[] ; numbers = new int[4] ;

  5. Creating an Array There are three steps to creating an array: public class Array { public static void main(String[] args) { int[] scores; scores = new int[3]; scores[0] = 10; scores[1] = 7; scores[2] = 9; } } 1. Declaration 2. Construction Initialization 3.

  6. Creating an ArrayDeclaration Declaring an array means providing a name and its data type: public class Array { public static void main(String[] args) { int[] numbers; char[] letters, symbols; long bigNumbers[]; String[] countries; } } Single declaration Multiple declaration Alternative declaration Array of objects

  7. Creating an ArrayConstruction Constructing an array means creating an object of its declared type: public class Array { public static void main(String[] args) { int[] numbers; char[] letters, symbols; long bigNumbers[]; String[] countries; numbers = new int[3]; String[] currencies = new String[3]; } } Create an array of int type consisting of 3 elements Declaration and construction at the same time

  8. Creating an ArrayInitialization Initializing an array means assigning values to its elements: public class Array { public static void main(String[] args) { int[] numbers; char[] letters, symbols; long bigNumbers[]; String[] countries; numbers = new int[3]; String[] currencies = new String[3]; numbers[0]=100; numbers[1]=200; numbers[2]=300; int[] newNumbers = {1,2,3}; } } Array index starts with 0 Declaration, construction, initialization at the same time

  9. Manipulating Arrays public class Array { public static void main(String[] args) { int[] numbers = new int[3]; numbers[0]=100; numbers[1]=200; numbers[2]=300; int[] newNumbers = {1,2,3}; for (int i=0; i<numbers.length; i++) { System.out.println(numbers[i]); } sumNumbers(numbers); sumNumbers(new int[]{3,2,1}); numbers = newNumbers; } static void sumNumbers(int[] n) { int sum=0; for (int i=0; i<n.length; i++) { sum += n[i]; } System.out.println(sum); } } length gives the size of the array Printing each element of an array Passing array to a method Assigning array to another array Passing anonymous array Sample Output 100 200 300 600 6

  10. Key Points • An array is a collection of values (either primitive or objects) all of which have the same data type • There are 3 steps in creating an array: • Declaration • Creation • Initialization • Array elements are accessed using an index that starts at 0 • length is the property of array that returns its size (i.e., number of elements) • Arrays can be passed to methods or assigned to variables

More Related