In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The purpose of this article is to share with you about the practice cases in JAVA. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
/ * define a method to implement the traversal of the array, and output the result [11 _ arr _ 356 _ 565 _ 889] int [] _ 4 _ 7}; the result contains a string, [], and the implementation steps are as follows: 1. Define a method to implement traversal of the array 2. First print [bracket 3. Traversing the element and comma of the output array of the array to determine whether it traverses the last element of the array, and if it is the last element, output] parentheses * / public class ArrayMethodTest {public static void main (String [] args) {int [] arr = {11arr2 44558 35066}; printArray (arr); int [] arr2 = {22Chart8899999935066}; printArray (arr2) } / * define method and implement function return value: void method parameter: array * / public static void printArray (int [] arr) {/ / output half parentheses, do not wrap to print System.out.print ("["); / / array to traverse for (int I = 0; I)
< arr.length ; i++){ //判断遍历到的元素,是不是数组的最后一个元素 //如何判断 循环变量 到达 length-1 if( i == arr.length-1 ){ //输出数组的元素和] System.out.print(arr[i]+"]"); }else{ //不是数组的最后一个元素,输出数组元素和逗号 System.out.print(arr[i]+","); } } System.out.println(); }}/* 数组的逆序: 数组中的元素,进行位置上的交换 逆序 不等于 反向遍历 就是数组中最远的两个索引,进行位置交换,实现数组的逆序 使用的是数组的指针思想,就是变量,思想,可以随时变换索引 反转 reverse 实现步骤: 1. 定义方法,实现数组的逆序 2. 遍历数组 实现数组的最远索引换位置 使用临时的第三方变量*/public class ArrayMethodTest_1{ public static void main(String[] args){ int[] arr = {3,5,7,1,0,9,-2}; //调用数组的逆序方法 reverse(arr); //看到数组的元素,遍历 printArray(arr); } /* 定义方法,实现数组的逆序 返回值: 没有返回值 参数: 数组就是参数 */ public static void reverse(int[] arr){ //利用循环,实现数组遍历,遍历过程中,最远端换位 //for的第一项,定义2个变量, 最后,两个变量++ -- for( int min = 0 , max = arr.length-1 ; min < max ; min++,max--){ //对数组中的元素,进行位置交换 //min索引和max索引的元素交换 //定义变量,保存min索引 int temp = arr[min]; //max索引上的元素,赋值给min索引 arr[min] = arr[max]; //临时变量,保存的数据,赋值到max索引上 arr[max] = temp; } } /* 定义方法,实现功能 返回值: void 方法参数: 数组 */ public static void printArray(int[] arr){ //输出一半中括号,不要换行打印 System.out.print("["); //数组进行遍历 for(int i = 0 ; i < arr.length ; i++){ //判断遍历到的元素,是不是数组的最后一个元素 //如何判断 循环变量 到达 length-1 if( i == arr.length-1 ){ //输出数组的元素和] System.out.print(arr[i]+"]"); }else{ //不是数组的最后一个元素,输出数组元素和逗号 System.out.print(arr[i]+","); } } System.out.println(); }}/* 数组的排序: 一般都是升序排列,元素,小到大的排列 两种排序的方式 选择排序: 数组的每个元素都进行比较 冒泡排序: 数组中相邻元素进行比较 规则: 比较大小,位置交换*/public class ArrayMethodTest_2{ public static void main(String[] args){ int[] arr = {3,1,4,2,56,7,0}; //调用选择排序方法 //selectSort(arr); //调用冒泡排序方法 bubbleSort(arr); printArray(arr); } /* 定义方法,实现数组的冒泡排序 返回值: 没有 参数: 数组 */ public static void bubbleSort(int[] arr){ for(int i = 0 ; i < arr.length - 1; i++){ //每次内循环的比较,从0索引开始, 每次都在递减 for(int j = 0 ; j < arr.length-i-1; j++){ //比较的索引,是j和j+1 if(arr[j] >Arr [juni1]) {int temp = arr [j]; arr [j] = arr [juni1]; arr [juni1] = temp }} / * define the method to achieve the array selection sort return value: no parameters: array implementation step: 1. The nested loop implements the sort outer loop, which controls how many inner loops are compared and how many elements are compared each time. Judge the size and value of an element and store it in a small index * / public static void selectSort (int [] arr) {for (int I = 0; I)
< arr.length - 1; i++){ //内循环,是每次都在减少,修改变量的定义 for(int j = i+1 ; j < arr.length ; j++){ //数组的元素进行判断 if(arr[i] >Arr [j]) {/ / array transposition int temp = arr [I]; arr [I] = arr [j]; arr [j] = temp }} / * define method and implement function return value: void method parameter: array * / public static void printArray (int [] arr) {/ / output half parentheses, do not wrap to print System.out.print ("[") / / Array to traverse for (int I = 0; I < arr.length) Length-1 if +) {/ / determine whether the traversal element is the last element of the array / / how to determine whether the loop variable reaches the element of the array (I = = arr.length-1) {/ / output array element and] System.out.print (arr [I] + "]") } else {/ / is not the last element of the array, outputs the array element and the comma System.out.print (arr [I] + ",");}} System.out.println () }} / * the lookup function of the array is to find an element in an array, whether it exists in the array, and if so, return the index ordinary query: find the index in which the element appears in the array. Without this element, the result is a negative number * / public class ArrayMethodTest_3 {public static void main (String [] args) {int [] arr = {1Co3. 5,7,9,11,15} Int index = binarySearch (arr,10); System.out.println (index);} / * define method, implementation, half-and-half search return value: index parameter: array, found element implementation step: 1. The required variables define three, three pointers 2. Condition min Intermediate Index small pointer = Intermediate + 1 element < Intermediate Index Big pointer = Intermediate-1 element = = Intermediate Index found, finished, returned to Intermediate Index 4. Loop ends and cannot be halved. Return-1 * / public static int binarySearch (int [] arr, int key) {/ / define three pointer variables int min = 0; int max = arr.length-1; int mid = 0; / / loop halved, conditional min
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.