It 18 Palm Job _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[].

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


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


*/



1,


Class Funtiondemo

{

/**

Auxiliary functions

*/

Print function

public static void Sop (String str)

{

System.out.println (str);

}

Print one-dimensional array

public static void PrintArray (int[] arr)

{

System.out.print ("[");

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

{

if (i==arr.length-1)

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

Else

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

}

System.out.println ();

}

Print two-dimensional arrays

public static void PrintArray (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 ();

}

}

Main function, the entry of the program

public static void Main (string[] args)

{

SOP ("The maximum value in the array is" +getmax (new int[]{1,2,9,3,7});

SOP ("Specifies the position of the element in the array as" +getindex (new int[]{1,2,9,3,7},7));

Int[] Arr =bubblesort (new int[]{1,2,9,3,7});

SOP ("sorted array follows:");

PrintArray (arr);

SOP ("Binary finds out the specified element position is:" +halfsearch (new int[]{1,2,9,3,7},9));

int[][] arr1 = new int[][] {{1,2,3},{4,5,6},{7,8,9}};

PrintArray (ARR1);

Transmatrix (ARR1);

SOP ("--------------following----------------");

PrintArray (ARR1);

}

/**

Job function

*/

1. Get the maximum value of the element in int[]

public static int Getmax (int[] arr)

{

if (arr==null| | arr.length==0)

{

SYSTEM.OUT.PRINTLN ("Array input exception");

return-1;

}

int temp = arr[0];

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

{

if (arr[i]>temp)

Temp=arr[i];

}

return temp;

}

2. Gets the location of the first occurrence of the specified element

public static int GetIndex (int[] arr,int key)

{

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

if (Arr[i]==key)

return i;

return-1;

}

3, define the function, complete the bubble sort, large number sinking.

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

{

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

{

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

{

int temp=0;

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

{

TEMP=ARR[J];

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

Arr[j+1]=temp;

}

}

}

return arr;

}

4. Binary Search

/*

Train of thought: Define Min,max,mid three array of angular marker variables, each time two points, the key and Arr[mid] value comparison

Key>arr[mid], the angle of key is between [Mid+1,max]

Key<arr[mid], the angle of key is between [min,mid-1]

Key=arr[mid], return to the mid-point mark,

The loop condition is Min<max, and when Min>max, the max-min= negative number indicates that the element was not found after two minutes.

*/

public static int Halfsearch (int[] arr,int key)

{

int mid,min=0,max=arr.length-1;

while (Min<=max)

{

Mid= (Min+max)/2;

if (Arr[mid]==key)

{

return mid;

}

else if (Arr[mid]<key)

{

min=mid+1;

}

Else

{

Max=mid-1;

}

}

return-1;

}

5. Matrix Transpose

/*

Idea: a matrix in a computer is actually a two-dimensional array arr[i][j]

I represents the row of the Matrix, and J represents the column of the Matrix.

The principle of the transpose operation is to swap the row and column of the elements on each position of the matrix

We know that the number of rows on the diagonal is equal,

*/

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

{

for (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;

}


It 18 Palm Job _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.