Basic Operations for arrays

Source: Internet
Author: User
Tags array length

1: Basic operation of the array

Get element: Element type variable = array name [index];

Set element: array name [index] = value;

To iterate over an array element:

for (int index = 0; Index <= nums. Length-1; index++) {

System. Out.println (nums[index]);

Array Length: array name. Length;length is a property not a method

Index range: increments from 0 onwards. [0, array name. length-1]

2: Common exceptions for manipulating arrays

nullpointerexception: null pointer exception (null reference): manipulate the array directly when the array is not initialized.

string[] bs = null;

System.out.println (Bs.length)

arrayindexoutofboundsexception : array index out-of-bounds exception.

3: Gets the maximum minimum element of the array

Get the largest element: Don't forget to pass Parameters!!!!

Public class ArrayDemo3 {

static int Getmax (int[] nums) {

int max = nums[0];

For (int index = 0; Index <= nums. Length-1; index + +)

{

If (nums[index] > Max)

{

Max = nums[index];

}

}

return max;

}

public static void Main (string[] args) {

// TODO Auto-generated method stub

int[] nums = new int[]{2,3,5,-3,4};

int max = Getmax (nums);

System. out.println (max);

}

}

4: Print array elements

When you print an array directly, the Hashcode value is printed.

static void PrintArray (string[] arr)

{

if (arr = = null)

{

System. OUT.PRINTLN ("null");

return;

}

String ret = "[";

For (int index = 0;index < arr. Length; index+ +)

{

ret = ret + arr[index];

//If the current index is not the last index, then stitch ","

if (index ! = arr.length-1)

{

ret = ret + ",";

}

}

ret = ret + "]";

System. out.println (ret);

}

In the Main method

string[] arr = {"a","B","C"};

PrintArray (arr);

5: Array elements in reverse order

Static string[] Reverse (string[] Oldarr)

{

string[] newarray = new string[Oldarr. Length]; //Create a new array to hold the inverted element

For (int index = Oldarr. Length-1;index >=0;index--)

{

newarray [Oldarr. Length-1-index] = Oldarr[index];

}

return newarray;

}

6: The element appears indexed (first/last time)

Int[] arr = {10,20,30,10,50,10,50}

Gets the index of the first occurrence of element 10 in the arr array (indexOf);

Gets the index of the last occurrence of element 10 in the arr array (lastIndexOf);

/*

* Query the location of the first occurrence of the key element in the Nums array,

Parameters

* Nums: from which array to do the query

* Key: The element of the current query

* Return: Returns the first occurrence of the index if key exists in the ARR array

* Key does not exist in the Nums array and returns -1.

*/

static int indexOf (int[] nums,int key)

{

For (int index = 0; Index <= nums. Length-1; index++)

{

If(nums[index] = = key)

{

return index;

}

}

return-1;

}

Last time

static int lastIndexOf (int[] nums,int key)

{

For (int index = nums. Length-1; index >= 0; index--)

{

If(nums[index] = = key)

{

return index;

}

}

return-1;

}

Basic Operations for arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.