Java Foundation Third Day _ array

Source: Internet
Author: User


/*


1. Define a function function that dynamically extracts the maximum value of an element in int[].

Class Demo

{


public static void Main (string[] arge) {

int[] array = new int[]{99,6,999,44};

System.out.println (Max (array));

}


public static int Max (int[] array) {

int temp = array[0];

for (int a=1; a<array.length; a++) {

if (Array[a] > Temp) {

temp = Array[a];

}

}

return temp;

}

}


*/


/*

2. Define a function to query the position of the first occurrence of the specified element from the array.

Class Demo {

public static void Main (string[] arge) {

System.out.println (Searchindex (New int[]{6,8,4,6,6,9,0,8}, 8));

}

public static int Searchindex (int[] array,int number) {

int index =-1;

for (int a=0; a<array.length; ++a) {

if (number = = Array[a]) {

index = A;

Break

}

}

return index;

}

}

*/


/*3. Define the function, complete the bubbling sort, and the large number sinks.


Class Demo {

public static void Main (string[] arge) {

int[] array = new int[]{4,2,88,1,-1,0,4};

Showarray (array);

}

public static int[] (int[] array) {

int temp = 0;

for (int a=0; a < Array.length-1;++a) {

for (int b=0; b < array.length-a-1; ++b) {

if (Array[b] > array[b+1]) {

temp = array[b];

ARRAY[B] = array[b+1];

ARRAY[B+1] = temp;

}

}

}

return array;

}

public static void Showarray (int[] array) {

for (int a=0; a<array.length; ++a) {

System.out.print (array[a]+ "");

}

}

}

*/



/*4. Binary Find.


Class Demo {

public static void Main (string[] arge) {

Int[] Array = {4, 6, 9, 22, 44, 89};

System.out.println (Showarray (array, 44));

}

public static int Showarray (int[] array, int number) {

int length = Array.Length;

int min = 0;

int max = Array.Length;

do{

int temp = (max+min)/2;

if (number = = Array[temp]) {

return temp;

}

if (number > Array[temp]) {

Min = temp + 1;

Continue

}

if (number < array[temp]) {

max = temp-1;

Continue

}

}while (TRUE);

}

}


*/




Define a function that implements the transpose of the matrix. arr[i][j] = = arr[j][i];//the precondition is affirmative.


/*

Class Demo {

public static void Main (string[] arge) {

Int[][] Array = {{1,2,3},{4,5,6},{7,8,9}};

Show (array);

Reverse (array);

Show (array);

}

public static void reverse (int[][] array) {

int temp = 0;

for (int a=0; a<array.length-1; ++a) {

for (int b=a+1; b<array[a].length;++b) {

temp = array[a][b];

ARRAY[A][B] = Array[b][a];

Array[b][a] = temp;

}

}

}

public static void Show (int[][] array) {

for (int a=0; a<array.length; ++a) {

for (int b=0; b<array[a].length;++b) {

System.out.print (array[a][b] + "\ t");

}

System.out.println ("");

}

}

}

*/


7. Traverse three-dimensional groups and output each layer of the three-dimensional array horizontally.


/*

Class Demo {

public static void Main (string[] arge) {

Int[][][] Array = {{1,2,3},{4,5,6},{7,8,9}},{{11,12,13},{14,15,16},{17,18,19}},{{31,32,33},{34,35,36},{37,38,39} }};

Show (array);

}

public static void Show (int[][][] array) {

for (int a=0; a<array.length; ++a) {

for (int b=0; b<array[a].length; ++b) {

for (int c=0; c < array[b].length; ++c) {

System.out.print (Array[b][a][c] + "\ t");

}

}

System.out.println ();

}

}

}

*/



7. Define a class: Dog has a name of color age Cry ();

For:

Class Dogdemo {

public static void Main (string[] args) {

Dog job = new Dog ("Job", 2);

System.out.println ("Name:" + job.getname () + "Age:" + job.getage ());

Job.setname ("Lala");

Job.setage (3);

System.out.println ("Name:" + job.getname () + "Age:" + job.getage ());

}

}


Class Dog {

private String name;

private int age;


public void Cry () {

System.out.println ("wangwang~!");

}


Public Dog (String name, int age) {

THIS.name = name;

This.age = age;

}


public void SetName (String name) {

THIS.name = name;

}


public void Setage (int.) {

This.age = age;

}


Public String GetName () {

return this.name;

}


public int getage () {

return this.age;

}

}


8. Elaboration

For:

1) Get the maximum value of the array, workaround: Iterate through the entire array and compare the maximum value by the IF condition

2) Query the array for a value, workaround: Traverse the entire array, if condition to determine, find this value, jump out of the loop

3) Bubble Sort: The minimum/maximum number, sinking to the end of the array to finish sorting.

The outer loop needs to be array.length-1 times, and the inner loop needs to be compared array.length-i-1 times.

4) Binary Lookup method: requires an ordered array, by comparing the median and lookup values, to determine the location of the lookup value (the middle value of the left or right), multiple loops to find the real place of the value.

5) Matrix transpose problem: involved in matrix initialization, assignment operation, transpose operation Note that only the value of the lower-left positive triangle should be exchanged array[i][j]=array[j][i].

6) Three-bit array: Abstract as a Rubik's cube, divided into layers, each row of each layer, each row of each column. With circular control, each layer can be printed horizontally and vertically. See the code for details.

7) Face object: involves the definition of the Face object class, the generation of objects, constructors, modifiers, javabean techniques, and the understanding of encapsulation.


9. Describe the heap area, stack area, when overflow, how to solve.

For:

Heap Area: Save object and member variable stack: Save method and local variable

Overflow condition: An object function that produces too much or consumes a large amount of memory recursive call itself may appear stack overflow

How to Solve: 1. Do not produce unnecessary objects or member variables 1. Be careful with recursive operations

2. Set the Java Virtual machine heap size (java-xms<size>) 2. Using non-recursive means


10.oop

For:

Face object: Is a programming method relative to the process, which simplifies the problem.

Class: Is an abstraction of an object.

Object: Is the concrete implementation of the class.

Instance: is the object.

Member variable: An object's property variable.

member function: The method of the object.

Public: Used to decorate a member variable or a member function, which represents a common use for other classes to invoke.

Private: Used to modify member variables or member functions, to represent private, for encapsulation, to improve data security, through the Set,get method to change the property, get

Constructor: Used to initialize an object. The function does not return a value.

This: is the member variable of the object, pointing to the current object, which is used in the class to refer to the current object.

Static: Statically, decorated member variables, shared by homogeneous objects, classes can also reference static members, static methods can only access static members.


Java Foundation Third Day _ array

Related Article

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.