1. Define a function function that dynamically extracts the maximum value of an element in int[].
Package Day2;public class Maxdemoi {public static void main (string[] args) {//TODO auto-generated method STUBSYSTEM.OUT.P Rintln (Getmax (new int[0])); int[] arr={1,2,3,4,5,6,7,8,9}; System.out.println ("Maxvalue is" +getmax (arr));} public static int Getmax (int[] arr) {if (arr==null| | arr.length==0) {System.out.println ("array not Present"); return-1;} int tmp=0;for (int i=0;i<arr.length;i++) {if (Tmp<arr[i]) {tmp=arr[i];}} return tmp;}}
2. Define a function to query the position of the first occurrence of the specified element from the array.
Package Day2;public class Firstshow {public static void main (string[] args) {//TODO auto-generated method stubint[] Arr=n EW Int[]{1,2,3,4,5};int Index=findarray (3, arr);} public static int Findarray (int num,int array[]) {if (array = = null| | array.length==0) {SYSTEM.OUT.PRINTLN ("Array cannot be empty"); return-1;} int index=-1;for (int i=0;i<array.length;i++) {if (array[i]==num) {index=i; System.out.println ("The position of the number taken is" +index);} Else{system.out.println ("Can't find Ah"); return-1;}} return index;}}
3. Define the function, complete the bubbling sort, and the large number sinks.
Package day2;public class bubbdemo { public void sort (Int[] args) { for (Int m : args) { system.out.print ("before " +args[m ]+ " "); } int time1 = 0,time2 = 0; for (int i = 0 ; i < args.length-1 ; i++) { ++ Time1; for (int j = i+1 ; j < args.length ; j+ +) { ++time2; int temp ; if (args [I] > args[j]) { temp = args[j]; args[j] = args[i]; args[i] = temp; } } } system.out.println (); system.out.println ("Outer loop number: Number of cycles in "+time1+": "+time2); for (Int n : args) { system.out.print ("After sorting " +n+ " "); } } public static void main (String[] args) { int[] arg = new int[]{2,1,4,5,8,7,6,3,9,0}; new bubbdemo (). sort (arg); }}
4. Binary find.
Package day2;/* * Binary Find */public class Mindemo {public static void main (string[] args) {//TODO auto-generated method Stubi Nt[] arr=new int[]{1,2,3,4,5,6,7,8,9};int a=0,b=arr.length-1,m=0;int x=5;while (a<=b) {m= (a+b)/2;if (m==x) { System.out.println ("find" +m); else if (X<arr[m]) {b=m-1;} else{a=m+1;}}}}
6. Define a function that implements the transpose of the matrix. arr[i][j] = = arr[j][i];//the precondition is affirmative.
Package day2;public class arraytransdemo {public static void main (String[] args) {// todo auto-generated method stubint[][] arr = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 },{ 11, 12, 13, 14, 15 }, { 16, 17, 18, 19, 20 },{ 21, 22, 23, 24, 25 } };out (arr); System.out.println ("------------"); Transarr (arr); out (arr);} Public static int[][] transarr (Int[][] arr) {// TODO Auto-generated Method stubfor (int i=0;i<arr.length-1;i++) {for (int j=i+1;j<arr[i].length;j++) {int Temp=arr[i][j];arr[i][j]=arr[j][i];arr[j][i]=temp;}} Return arr;} Private static void out (Int[][] arr) {// todo auto-generated method stubfor (int i=0;i<arr.length;i++) {for (int j=0;j<arr[i].length;j++) System.out.println (arr[i][j]+ "\ t");} System.out.println ();}}
7. Traverse three-dimensional groups and output each layer of the three-dimensional array horizontally.
Package Day2;public class Outhorizontaldemo {public static void main (string[] args) {//TODO auto-generated method Stubint [][][] arrr={{{1,2,3},{4,5,6},{7,8,9}},{{10,11,12},{13,14,15,},{16,17,18}},{{19,20,21},{22,23,24},{25,26,27}} };outarrr (ARRR);} private static void Outarrr (int[][][] arrr) {//TODO auto-generated method stubfor (int i=0;i<arrr.length;i++) {for (int j=0;j<arrr[i].length;j++) {for (int t=0;t<arrr[i][j].length;t++) {System.out.print (arrr[j][i][t] + "\ t");} System.out.print ("|");} System.out.println ();}}}
8. Define a class: Dog has a name of color age Cry ();
package day2;public class dogdemo {public Static void main (String[] args) {// todo auto-generated method stubdog dog=new dog ("Houhou", 3);d og.cry (); System.out.println ("name" +dog.getname () + "Age" +dog.getage ());d Og.setage (4);d og.setname ("Lallalalal"); System.out.println ("name" +dog.getname () + "Age" +dog.getage ());}} Class dog{private string name;private int age;public dog (String name,int age) {this.name=name;this.age=age;} Public string getname () {return name;} Public void setname (String name) {this.name = name;} Public int getage () {return age;} Public void setage (Int age) {this.age = age;} Public void cry () {System.out.println ("55555555555");}}
9. Describe the heap area, stack area, when overflow, how to solve.
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
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.
Big Data job The third day