Java - Arrays

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Returning an Array from a Method

public static int[] reverse(int[] list) { int[] result = new int[list.length]; for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; }

Passing Arrays to Methods

you can pass primitive type values to methods, you can also pass arrays to methods. public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } } or can invoke it by passing an array. printArray(new int[]{3, 1, 2, 6, 4, 2});

Processing Arrays

"for loop or foreach loop" - because all of the elements in an array are of the same type and the size of the array is known.

Array

- stores a fixed-size sequential collection of elements of the same type. - used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

index

The array elements are accessed through the _______ - Array indices are 0-based, they start from 0 to arrayRefVar.length-1.

Creating Arrays

arrayRefVar = new dataType[arraySize]; The above statement does two things − - It creates an array using new dataType[arraySize]. - It assigns the reference of the newly created array to the variable arrayRefVar.

The Arrays Class : The java.util.Arrays class

contains various static methods for sorting and searching arrays, comparing arrays, and filling array elements. These methods are overloaded for all primitive types. -public static int binarySearch(Object[] a, Object key) -public static boolean equals(long[] a, long[] a2) -public static void fill(int[] a, int val) -public static void sort(Object[] a)

Declaring Array Variables

dataType[ ] arrayRefVar; Ex : double[ ] myList;


Set pelajaran terkait

Chapter 13 SB: Obesity, Energy Balance, and Weight Management

View Set

principles of Growth and Development

View Set

MicroBiology Chapter 2 Study Guide

View Set

Risk Management and Insurance test 1

View Set

Medications for Respiratory system

View Set

Project Management- Business Analyst

View Set

Lewis Ch 4 - Patient and Caregiver teaching

View Set