The third day of basic Java learning

Source: Internet
Author: User

Array

----------------------------------------

1. Container, same type

2. Using subscript, 0 as base address

3. Defining arrays

Type [] name = new Type [number of elements or array length];

int [] arr = new Int[5]

Type [] name = new Type []{, element, element,....}; is initialized directly.

4.

5.

6.

7. Do not empty or cross-border when using arrays


Heap: Heap

----------------------

Object Head, heap of objects


Stack: Stack

------------------------

Method stack. The corresponding thread, with at least one main thread

The first in the stack is the main function, and then call the function on the main function, which is called the pressure stack, pressing each box called method frame.


Advanced post-out, push stack: Stack, pop stack: Reload


Java without pointers is a pointer operation that is not displayed. A reference is a pointer.


Memory structure:

-----------------------------

When a Java program runs, it needs to allocate space in memory.

1. Stack Memory:

Used to store local variables, and when finished, the occupied space is automatically released


2. Heap Memory:

Arrays and objects are placed in heap memory by the instances created by new.

Each entity has a memory address value

The variables in the entity have default initialization values

Entities are not being used and are reclaimed by the garbage collector for an indeterminate period of time




Array Operations FAQ

-----------------------------------

1. Array pin out-of-bounds exception (arrayindexoutofboundsexception)

int[] arr = new int[2];

System.out.println (Arr[3]);

Occurs when a non-existent script in an array is accessed

2. Null pointer exception (NULLPOINTEREXCEPTION)

int[] arr = null;

System.out.println (Arr[0]);

The ARR reference does not point to the entity, but the element in the action entity times is wrong.

Compile without error, run times error, call run exception (runtimeexception)


Example:

Take the maximum value in an array

------------------------------------------------

/*

Determining the maximum value in an array

Defines a method that passes an array into the method and determines the maximum value.

Requires robustness of the method to detect null values and arrays of length 0

*/



Class ArrayMax

{

public static void Main (string[] x)

{

System.out.println (Getmax (new int[0]));

}


Defining methods

public static int Getmax (int[] arr)

{

if (arr = = NULL | | arr.length = = 0)//Determine if the value passed is a null value | | The (or OR) array length is 0

{

SYSTEM.OUT.PRINTLN ("Array does not exist"); An array error that satisfies the condition description, printing error message

return 0;

}

int temp = Integer.min_value; Assigns an integer value to a temporary variable, Integer.min_value is a class of integral type that represents the minimum value

for (int i = 0; i < arr.length; i + +)//define variables to be used as array tags, depending on the length of the array (arr.length) to determine the number of loops, the number of the following tag in the array starts from 0.

{

if (temp < arr[i])//Judge the value in the temporary variable to be compared to the number of elements in Group I,

{

temp = Arr[i]; Assigns the value in the array primitive to the TEMP variable if it is less than

}

}

return temp;

}

}

---------------------------------------------------

Example 2

Sort

1. Select sort

---------------------------------

/*

Sort an array

Defines a method that passes an array into the method and sorts it.

Requires robustness of the method to detect null values and arrays of length 0

*/



Class Arrayselectsort

{

public static void Main (string[] x)

{

Outarr (sort (new int[] {9,1,4,3,5,11,54,22}));

}


Defining methods

public static int[] Sort (int[] arr)

{

if (arr = = NULL | | arr.length = = 0)//Determine if the value passed is a null value | | The (or OR) array length is 0

{

SYSTEM.OUT.PRINTLN ("Array does not exist"); An array error that satisfies the condition description, printing error message

}

Each element of an iterated group

for (int i = 0; i < arr.length; i + +)//define variables to be used as array tags, depending on the length of the array (arr.length) to determine the number of loops, the number of the following tag in the array starts from 0.

{

Define minimum value

int min = Integer.max_value;

int index = 0;


Find subscript for minimum and minimum values

for (int j = i + 1; j < Arr.length; J + +)

{

if (min > Arr[j])

{

min = Arr[j];

index = j;

System.out.println (min);

}

}


Completes the swap of the smallest element in the current element and subsequent arrays

int xx = 0;

if (Arr[i] > min)

{

Arr[index] = Arr[i];

Arr[i] = min;

}

}

return arr;

}


Defines each element of the output array that is printed according to the subscript ordinal

public static void Outarr (int[] arr)

{

for (int i = 0; i < arr.length; i + +)

{

System.out.print (Arr[i] + "");

}

}

}



----------------------------------

3. Bubble sort





This article is from the "Edel" blog, make sure to keep this source http://adairh.blog.51cto.com/9436392/1847038

The third day of basic Java learning

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.