Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common applications of Java array

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are the common applications of Java array". In the daily operation, I believe that many people have doubts about the common application of Java array. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "what are the common applications of Java array?" Next, please follow the editor to study!

Double for cycle

Outer loop control row, inner loop control column.

/ / multiplication table for (int I = 1; I arr [juni1]) {temp = arr [j]; arr [j] = arr [juni1]; arr [juni1] = temp;} for (int n: arr) {System.out.println (n);}}}

Select sort

Package day4;import java.util.Arrays;public class Demo13 {public static void main (String [] args) {/ / Select sort int [] arr = {5, 12, 10, 3, 7, 8345}; int temp; for (int I = 0; I

< arr.length-1; i++) {//位置 for(int j = i + 1; j < arr.length; j++) { if(arr[i] >

Arr [j]) {temp = arr [I]; arr [I] = arr [j]; arr [j] = temp;} for (int n:arr) {System.out.println (n);} / / System.out.println (Arrays.toString (arr));}}

Array search method ordinary search method (low efficiency)

Public class ArrayTest {public static void main (String [] args) {/ / TODO Auto-generated method stub int [] arr = {4 for (int [] arr,int key) {for (int iMagisti) {for

Binary search method (high efficiency)

Public class binarySearch {public static void main (String [] args) {int [] arr = {9 arr,12 12, 15 24, 36 41, 59 68}; int num = searchArray (arr,12); System.out.println (num);} / / binary search. The day before yesterday: the array must be ordered. / * ideas: * 1. Get the element * 2 on the middle corner first through the angle mark. Compare this element with the data you are looking for. * 3. If the number of looking for is large, narrow the scope, the scope should be the middle corner + 1 color color-tail corner * if you are looking for the number efficiency, look for the range of head corner-middle corner-1. * 4. If you repeat this over and over again, you can find the corner mark corresponding to the element. * * / public static int searchArray (int [] arr,int key) {int max,min,mid; min = 0; max = arr.length-1; mid = (min+max) > > 1; while (arr [mid]! = key) {if (key > arr [mid]) {min = mid + 1;} else {max = mid-1;} / determine whether the element exists. If (max > 1;} return mid;}}

Public class ArrayTest1 {public static void main (String [] args) {int [] arr = {9 arr,44 12, 15 24, 36 41, 59 68}; int num = binarySearch (arr,44); System.out.println (num);} / / binary search. The day before yesterday: the array must be ordered. / * ideas: * 1. Get the element * 2 on the middle corner first through the angle mark. Compare this element with the data you are looking for. * 3. If the number of looking for is large, narrow the scope, the scope should be the middle corner + 1 color color-tail corner * if you are looking for the number efficiency, look for the range of head corner-middle corner-1. * 4. If you repeat this over and over again, you can find the corner mark corresponding to the element. * / public static int binarySearch (int [] arr,int key) {/ / define a single variable, record head corner, tail corner, middle corner int max,min,mid; min = 0; max = arr.length-1; while (min > 1; if (key > arr [mid]) {min = mid + 1;} else if (key < arr [mid]) {max = mid-1;} else {return mid;}} return-1;}}

Replication of array

Package cn.java.study;import java.util.Arrays;public class Demo5 {public static void main (String [] args) {int [] arr1 = {1 Arrays.toString (arr2)); int [] arr2 = new int [arr1.length]; / / Source array start position target array start position System.arraycopy (arr1, 1, arr2, 2, 3); System.out.println (Arrays.toString (arr2));}}

Arrays classes of commonly used API

The package cn.java.study;// utility class import java.util.Arrays;public class Demo5 {public static void main (String [] args) {/ / Arrays int [] arr = {1meme4p334 System.out.println 55}; / / convert the array to a string System.out.println (Arrays.toString (arr)); / / sort the array in ascending order Arrays.sort (arr); System.out.println (Arrays.toString (arr)) / / sort array name sort array element start position sort array element end position (in fact, take the end position minus one) [start position, end position) Arrays.sort (arr,2,4); System.out.println (Arrays.toString (arr)); / / parallel operations under multicore processors use Arrays.parallelSort (arr) / / binary search subscript, the number looked up in the array, returns the insertion point, if not, the negative insertion point minus one value System.out.println (Arrays.binarySearch (arr, 8)); / / array comparison: the number of elements is the same as the array elements in the corresponding position int [] arr1 = {1mer2jue 5}; int [] arr2 = {1jin2jin5}; System.out.println (Arrays.equals (arr1, arr2)) / / the filling of the array, replacing all the elements in the array with 666 Arrays.fill (arr, 666); System.out.println (Arrays.toString (arr)); / / the replication of the array returns an array, (array to be copied, several elements) int [] arr3 = Arrays.copyOf (arr1, 2); System.out.println (Arrays.toString (arr3));}}

Two-dimensional array

Format 1:

Int [] [] arr = new int [3] [2]

It is defined that there are three one-dimensional arrays in a two-digit array named arr, and there are two elements in each one-dimensional array. The names of the one-dimensional arrays are arr [0], arr [1], arr [2] respectively. The first one-dimensional array is assigned a value of 666 to the first one-dimensional array as follows: arr [0] [1] = 666.

System.out.println (arr); / / [[Ie6f7d2 binary array entity e6f7d2 is a hash, [is an array, [[two-digit array]

Format 2:

Int [] [] arr = new int [3] []; arr [0] = new int [3]; arr [1] = new int [1]; arr [2] = new int [2]

At this point, the study of "what are the common applications of Java arrays" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report