18 Palm Third Day Course summary

Source: Internet
Author: User

Learning content

    1. The form of an array, the internal storage structure of the array

    2. Select sort, bubble sort, binary find

Homework after class

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

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

3. Define the function, complete the bubble sort, and sink the large number.

4. Binary find.

5. Elaboration

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

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

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

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

10.oop

Answer:

1 answers: Maximum values of elements in dynamically extracting int[]

Class demo{

public static void Main (string[] args) {

Outresult (Getmax (new int []{1,6,4,2}));

}

To find the maximum of the number of groups

public static int Getmax (int array[]) {

int temp = Integer.min_value;


if (array = = NULL | | array.length = = 0) {

SYSTEM.OUT.PRINTLN ("Invalid input array");

return 0;

}

for (int i = 0; i < Array.Length; i++) {

if (temp < array[i]) {

temp = Array[i];


}

}

return temp;

}


public static void Outresult (int i) {

System.out.println (i);;

}

}

2 Answer: Query the location of the first occurrence of the specified element from the array

Class demo{

public static void Main (string[] args) {

Numlocation (New int[]{1,6,4,5,8,3});

}

public static void numlocation (int num, int arr[]) {

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

if (num = = Arr[i]) {

SYSTEM.OUT.PRINTLN ("This element first appears in the" + (i + 1) + "position");

Break

}else if (num! = arr[i] && i = = (arr.length-1)) {

SYSTEM.OUT.PRINTLN ("The element does not exist in the array");

}

}

}

}

3 answers: Bubble sort, large number sinking

Class demo{

public static void Main (string[] args) {

Outarr (Bubblesort (New int[]{3,6,10,9,2}));

}

public static int[] Bubblesort (int arr[]) {

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

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

int temp = 0;

if (Arr[j] > arr[j+1]) {

temp = Arr[j];

ARR[J] = arr[j+1];

ARR[J+1] = temp;

}

}

}

return arr;

}

public static void Outarr (int arr[]) {

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

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

}

}

}

4 answers: Binary find

Class demo{

public static void Main (string[] args) {

System.out.println (Halfbind (new int[]{1,2,3,4,7,9,10},4));

}

Binary find

public static int halfbind (int arr[],int num) {

int a = 0, B = arr.length-1, M = 0;

int midindex = 0; Subscript of the middle number

while (a <= b) {

Midindex = (A + b)/2;

m = Arr[midindex];

if (num = = m) {

return midindex;

}else if (num < m) {//fall to the left of the middle number

b = midIndex-1;

}else{//fall to the right of the middle number

A = Midindex + 1;

}

}

return-1;

}

6. Answer: Transpose matrix

Class demo{

public static void Main (string[] args) {

int num = 1;

int[][] arr = new INT[4][4];

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

for (int j = 0; J < Arr[i].length; J + +) {

ARR[I][J] = num;

num + +;

}

}

Outarr (Tranarr (arr));

}

Transpose matrix

public static int[][] Tranarr (int arr[][]) {

int temp = 0;

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

for (int j = i; J < Arr[i].length; J + +) {

temp = Arr[i][j];

ARR[I][J] = Arr[j][i];

Arr[j][i] = temp;

}

}

return arr;

}

Output matrix

public static void Outarr (int arr[][]) {

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

for (int j = 0; J < Arr[i].length; J + +) {

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

}

System.out.println ();

}

}

}

7. Answer: Horizontal output three-dimensional array of each layer

Class demo{

public static void Main (string[] args) {

Outarr (Getarr3_3_3 ());

}

Constructing three-dimensional arrays

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

int num = 1;

int[][][] arr = new INT[3][3][3];

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

for (int j = 0; J < Arr[i].length; J + +) {

for (int k = 0; k < arr[i][j].length; k++) {

ARR[I][J][K] = num;

num + +;

}

}

}

return arr;

}

Output matrix output by column

public static void Outarr (int arr[][][]) {

Find each level in turn

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

Find the columns in the first row of each level in turn output

for (int k = 0; k < arr[i][0].length; k++) {

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

}

System.out.print ("|" + "");

}

System.out.println ();

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

Find the columns of the second row of the first level in turn output

for (int k = 0; k < arr[i][1].length; k++) {

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

}

System.out.print ("|" + "");

}

System.out.println ();

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

Find each column of the third row of the first level output sequentially

for (int k = 0; k < arr[i][2].length; k++) {

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

}

System.out.print ("|" + "");

}

System.out.println ();

}

}

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

Output by row is traversed directly, simpler

8. Create a Dog class

Class demo{

public static void Main (string[] args) {

Dog dog = new Dog ();

Dog.setage (3);

Dog.setcolor ("Black");

Dog.cry ();

}

}

Class dog{

private String color;

private int age;

public void SetColor (String color) {

This.color = color;

}

Public String GetColor () {

return color;

}

public void Setage (int.) {

This.age = age;

}

public int getage () {

return age;

}

public void Cry () {

System.out.println ("Wang Bark");

}

}

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

Stack Overflow Scenario:

When continuous methods are constantly running in the form of recursion, a stack overflow often occurs when the buyers is pressed.

Workaround: When the JVM memory is sufficient, JAVA-XSS can be used to reset the stack memory to resolve

Heap Overflow Scenario:

Heap overflow occurs when other ways such as creating an array or an object are too large to be written to the heap memory.

Workaround: When the JVM memory is sufficient, JAVA-XMX can be used to reset the heap memory to resolve

Problems encountered


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

    • Hint encoding GBK the non-mapped characters need to be javac-encoding utf-8 Xxx.java to compile

In fact, we just set the notepad++ language into GBK Chinese form to solve the problem perfectly.


    • Array new is in the wrong form

the three types of arrays are constructed:

int []arr = new int[3];   int []arr = new int[]{1,3,4,5}; int[]arr = {1,4,2,5};

    • Integer.min_value is wrong to write Integer.min_value.

2. Select sort start understanding is not very thorough, think about it is very clear

3. When compiling a query from an array for the first occurrence of the specified element, the error is:

The original is missing int, should be new int[]{1,3,5,2,9};


Class demo{

public static void Main (string[] args) {

Outresult (Getselectsort (new int []{1,6,4,2}));

}

Select sort

public static int[] Getselectsort (int arr[]) {

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

Gets the minimum value of all values following the current value of the array

int min = Integer.max_value;

int num = 0;

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

if (min > Arr[j]) {

min = Arr[j];

num = j;

}

}

Determines the value and ordinal of the current value and the minimum value after the interchange

if (Arr[i] > min) {

Arr[num] = Arr[i];

Arr[i] = min;

}

}

return arr;

}

public static void Outresult (int arr[]) {

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

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

}

}

}

Attached Teacher Code:

Class Arrayselectsort

{

public static void Main (string[] x) {

Outarr (Sort (new int[]{5,7,8,1,3,4,9}));

}

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

Each element of an iterated group

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

Define minimum value

int min = Integer.max_value;

int index = 0;

Find the lowest and lowest values for subscript

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) {

xx = Arr[i];

Arr[index] = Arr[i];

Arr[i] = min;

}

}

return arr;

}

public static void Outarr (int[] arr) {

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

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

}

}

}

4. Write for loop often directly i = 0; should be written as int i = 0;

5. Transpose The matrix is always not fully transpose

The reason is that the code for Exchange Arr[i] [j] and Arr[j][i] is incorrect, and note the starting position of the second layer loop

6.650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/> is actually very simple, just let the number of rows and columns of each row equal

7. In the output three-dimensional array times wrong, after the discovery Getarr3*3*3 () This identifier is not legal

8. Always error when creating the Dog class:

Later found one more (), and class dog can not be placed in another class


18 Palm Third Day Course summary

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.