1 / 18

Arrays http://java.sun.com/

Arrays http://java.sun.com/. Writing a program that uses a large amount of information. Such as a list of 100 elements. It is not practical to declare variables for each piece of data. Arrays . Arrays solve this problem Letting us declare one variable.

khuyen
Download Presentation

Arrays http://java.sun.com/

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. Arrays http://java.sun.com/ • Writing a program that uses a large amount of information. • Such as a list of 100 elements. • It is not practical to declare variables for each piece of data.

  2. Arrays • Arrays solve this problem • Letting us declare one variable. • This can hold multiple accessible values.

  3. Array Indexing • An array is a list of values. • Each value is stored at a specific, position. • To access a value, we use the name of the array.

  4. Array Indexing • The total of elements is called the size. • An array of size “N” is indexed from 0 to N-1.

  5. Declaring and using arrays In Java, arrays are objects. To create an array, the reference must be declared. The array can then be created using the new operator.

  6. Code Example: int [ ] height = new int [10] ; The variable height is declared to be an array. The creation of height, using the newoperator.

  7. [ ] Square Brackets The square brackets used to indicate the index. The index performs automatic bounds checking. Bounds checking ensures that the index is in the range.

  8. Example: An array calledprices is created with 25 elements. The value of the index is checked from 0 to 24. Otherwise an exception is called. “ArrayIndexOutOfBoundsException.”

  9. length The size of the array is held in a constant called length. After the array prices is created with 25 elements, the constant prices.length contains the value 25.

  10. Example: for ( int i = 0; i < prices.length; i++ ) { System.out.println( prices[ i ] ); }

  11. Array syntax First is to associate the brackets with the type of values stored in the array: int [ ] grades ; Second is to associate the brackets with the name of the array: int grades [ ] ;

  12. Initializing arrays A technique for instantiating arrays is using an initializer list, which provides the initial values for the array. The items in an initializer list are separated by commas and delimited by braces. The size of the array is determined by the number of items.

  13. Example of initializer list When an initializer list is used, the new operator is not used. This declaration instantiates the array grades as an array of six integers, indexed from 0 to 5: int [ ] grades = { 87, 98, 69, 99, 88, 76 }

  14. Arrays as parameters Because an array is an object, when an array is passed as a parameter, a copy of the reference to the original array is passed. A method that receives an array as a parameter can permanently change an element of the array because it is referring to the original value.

  15. Arrays of objects Arrays store primitive typessuch as integers and characters. Arrays can also store references to objects as elements. An array could contain objects that consist of several variables and the methods that use them.

  16. Example: for ( int port = 0; port <= 4350; port++ ) { try { ServerSocket server = new ServerSocket( port ); } catch (IOException e) { System.out.println( “There is a server on port: “ + port + “ . “ );

  17. Arrays of string objects The variable words is an array of references to String objects. String [ ] words = new String [ 25 ] ; The new operator in the declaration instantiates the array and reserves space for 25 String references.

  18. Two-dimensional arrays Two-dimensional array has values in two directions, which are often represented as the rows and columns of a table. The type of a two-dimensional array that stores integers is: int [ ] [ ] ;

More Related