A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://howtodoinjava.com/java/array/intro-to-arrays/ below:

Java Array (with Examples)

An array is a container object that holds a fixed number of values of a single type in a contiguous memory location. It is a data structure that is used to store a finite number of elements, and all elements must be of the same data type.

Arrays are index-based data structures that allow random access to elements, they store. Indices start with '0'.

1. Array Representation in Memory

In this example, we have created an array of 5 elements. Indexes will range from '0' to '4'.

int[] a = new int[5];
 
a[0] = 1;
a[1] = 2;
a[2] = 4;
a[3] = 8;
a[4] = 16;

A pictorial representation of the above example can be as below.

2. Features of an Array 3. Types of Arrays

An array can be either of the following two types:

3.1. Single-Dimensional Array

An array that stores only primitives or objects is called a single-dimensional array. The general form of a one-dimensional array declaration is:

type var-name[];
OR
type[] var-name;
 
//Examples
 
int[] numbers;
 
String names[];
3.2. Multi-Dimensional Array

A multi-dimensional array stores other arrays.

It is array of arrays. In a multi-dimensional array, each array element holds the reference of other arrays. A multidimensional array is created by appending one set of square brackets ([ ]) per dimension.

type var-name[][];
OR
type[][] var-name;
 
//Examples
 
int[][] cordinates;
 
String nameSets[][];
4. Basic Operations on Arrays 4.1. Initializing an Array

The syntax for creating an array with pre-defined values.

String status[] = { "Active", "Inactive", "Purged" };

//or

String status[] = new String[] { "Active", "Inactive", "Purged" };
4.2. Iterating over Items

Use the standard for-each loop to iterate over the items of an array.

String status[] = { "Active", "Inactive", "Purged" };

for(String s : status) 
{
        System.out.println(s);
}
4.3. Printing Arrays

The recommended way to print the content of a simple array is using Arrays.toString().

System.out.println( Arrays.toString( status ) );

Use Arrays.deepToString() to print multi-dimentional arrays.

System.out.println(Arrays.deepToString( arrayOfArray )); 
4.4. Finding Max and Min

The Stream interface provides two methods max() and min() that return the largest and the smallest item from the underlying stream. We can use these methods on the stream obtained from an Array.

int max = Arrays.stream(arrayOfInts)
  .max()
  .getAsInt(); 

int min = Arrays.stream(arrayOfInts)
  .min()
  .getAsInt(); 
5. Conclusion

In this Java Array tutorial, we learned the basic characteristics of arrays in Java. We also learned to perform very basic operations on array items. You can refer to these array tutorials to enhance your knowledge.

Happy Learning !!

Sourcecode on Github


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4