Dark Horse programmer-Java basics-functions and Arrays

Source: Internet
Author: User

1. Function

A function is an independent small segment defined in a class with specific functions.ProgramIs also called a method.

Format:Modifier Return Value Type Function Name (parameter type form parameter 1, parameter type form parameter 2 ,......)

{

Execute the statement;

Return return value;

}

Generally, to improveCodeOfReusability, We can run the codeExtraction, Encapsulated as an independent function, which can be called directly when it is large. If the function does not return a specific value, the return value type is expressed by the keyword void, so the return statement in the function can be omitted in the last line without writing. MainNote:Yes: the function can only be called. You cannot define a function in the function. when defining a function, the function result should be returned to the caller and submitted to the caller for processing.

Check the Code:

Class functiondemo {

Public static void main (string [] ARGs ){

Getresult (5 );

}

Public static void getresult (INT num ){// This is the encapsulated function, which improves the reusability and scalability of the function. Its function is to calculate a number multiplied by 3 plus 5, void is not

System. Out. println (Num * 3 + 5 );// Return results

Return; // can be omitted}

}

}

How to define a function?
1. Since a function is an independent function, the calculation result of this function is clear first, because it is clear about the return value type of the function.
2. Determine whether unknown content is required for calculation when defining this function. Because the parameter list of the function is specified (the parameter type and number of parameters ).

Let's take a look at the Code:

Class functiontest {

Public static void main (string [] ARGs ){

Draw (5, 6 );

Printhr ();

Draw (7, 9 );

Printhr ();

Print99 ();

}

// Define a function for printing 99 multiplication tables.

Public static void print99 ()
{
For (INT x = 1; x <= 9; X ++)
{
For (INT y = 1; y <= x; y ++)
{
System. Out. Print (Y + "*" + x + "=" + y * x + "\ t ");
}
System. Out. println ();
}
}

Public static void draw (INT row, int col) // you can print a rectangle.
{
For (INT x = 0; x <row; X ++)
{
For (INT y = 0; y <Col; y ++)
{
System. Out. Print ("*");
}
System. Out. println ();
}
}

Public static void printhr () // print the horizontal line
{
System. Out. println ("------------------------------");
}

}

Function Overloading: The function name is the same. Changing the parameter list is only related to the two. See the following example.

Class functionoverload {

Public static void main (string [] ARGs ){

Add (4, 5 );

Add (4, 5, 6 );

}

// Define an addition operation to obtain the sum of two integers.

Public static int add (int x, int y ){

Return X + Y;

}

// Define an addition to obtain the sum of three integers.

Public static int add (int x, int y, int Z ){

Return add (x, y) + z;

}

}

When to use overload?
When the defined functions are the same, but the unknown content involved in the operation is different.
Then, a function name is defined to indicate the function to facilitate reading. Multiple Functions with the same name are distinguished by different parameter lists.

2, array

An array is a collection of the same type of data. An array is a container that automatically numbers the elements in the array from 0 for convenient operations.

Statement:

Element type [] array name = new element type [number of elements or array length]; int [] arr = new int [5];

Element type [] array name = new element type [] {element 1, element 2 ,......}; Int [] arr = new int [] {1, 2, 3, 4, 5 };

Array initialization:

Static initialization: when creating an array, the programmer explicitly assigns an initial value to the element in the array. You do not specify the array length int [] arr = {1, 2, 3, 4, 5 };Int [] arr1 = new int [] {1, 2, 3, 4, 5 };

Dynamic initialization: when creating an array, the system assigns an initial value to the elements in the array by default. you specify the array length int [] A; A = new int [3];

Default Value of array elements: 0 for int type, 0.0 for float, and null for string type

Array exception:

Array out-of-bounds exception: When the array is operated, the badge that does not exist in the array is accessed. arrayindexoutofboundsexception

Null Pointer exception: when the reference does not point to a null value, the reference is still used to operate the object, nullpointerexception

Get the length of the array:

Array name. Length System. Out. println ("Length:" + arr. Length );

To obtain the length of an array, traversal is usually used.There is a question: Separate the elements of the array with commas.

Public static void printarray (INT [] ARR ){

System. Out. Print ("[");

For (INT x = 0; x <arr. length; X ++ ){

If (X! = Arr. Length-1)

System. Out. Print (ARR [x] + ",");

Else

System. Out. println (ARR [x] + "]");

}

}

Sorting of Arrays

Public static void selectsort (INT [] ARR)
{
For (INT x = 0; x <arr. Length-1; X ++)
{
For (INT y = x + 1; y <arr. length; y ++)
{
If (ARR [x]> arr [y])
{
Swap (ARR, x, y );
}
}
}
}

 

Public static void bubblesort (INT [] ARR)
{
For (INT x = 0; x <arr. Length-1; X ++)
{
For (INT y = 0; y <arr. length-x-1; y ++) //-X: reduce the elements for each comparison,-1: avoid cross-border corner.
{
If (ARR [y] <arr [Y + 1])
{
Swap (ARR, Y, Y + 1 );
}
}
}
}

 

Public static void swap (INT [] arr, int A, int B)
{
Int temp = arr [a];
Arr [a] = arr [B];
Arr [B] = temp;
}

 

Public static void printarray (INT [] ARR ){

System. Out. Print ("[");

For (INT x = 0; x <arr. length; X ++ ){

If (X! = Arr. Length-1)

System. Out. Print (ARR [x] + ",");

Else

System. Out. println (ARR [x] + "]");

}

}

Array Inversion

Public static void reversearray (INT [] ARR)
{
For (INT start = 0, end = arr. Length-1; Start <end; Start ++, end --)
{
Swap (ARR, start, end );
}
}

Public static void swap (INT [] arr, int A, int B)
{
Int temp = arr [a];
Arr [a] = arr [B];
Arr [B] = temp;
}

 

Define the function to obtain the position where the key appears in the array for the first time. If the returned value is-1, the key does not exist in the array.

Public static int getindex (INT [] arr, int key)
{
For (INT x = 0; x <arr. length; X ++)
{
If (ARR [x] = key)
Return X;
}
Return-1;
}

 

Public static int getindex_2 (INT [] arr, int key ){

Int min = 0, max = arr. Length-1, mid;

While (Min <= max ){

Mid = (MAX + min)> 1;

If (Key> arr [Mid])

Min = Mid + 1;

Else if (Key <arr [Mid])

Max = mid-1;

Else

Return mid;

}

Return min;

}

 

 

Half Lookup

Public static int halfsearch (INT [] arr, int key ){

Int min, Max, mid; min = 0;

Max = arr. Length-1;

Mid = (MAX + min)/2;

While (ARR [Mid]! = Key ){

If (Key> arr [Mid])

Min = Mid + 1;

Else if (Key <arr [Mid])

Max = mid-1;

If (min> MAX)

Return-1;

Mid = (MAX + min)/2;

}

Return mid;

}

 

Public static int halfsearch_2 (INT [] arr, int key ){

Int min = 0, max = arr. Length-1, mid;

While (Min <= max ){

Mid = (MAX + min)> 1;

If (Key> arr [Mid])

Min = Mid + 1;

Else if (Key <arr [Mid])

Max = mid-1;

Else

Return mid;

}

Return-1;

}

 

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.