In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about the usefulness of arrays 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.
Java array 1, the definition of an array is an ordered collection of the same type of data an array describes several data of the same type, sorted and combined in a certain order, each data is called an array element, and each array element can be created by subscript access to its 1.1, array declaration
You must declare array variables before you can use arrays in your program.
The Java language uses the new operator to create arrays, and the syntax is as follows
DataType [] arrayRefVar; / / preferred dataType arrayRefVar []; / / same effect, but not preferred
The elements of the array are accessed through the index, which starts at 0
DataType [] arrayRefVar = new dataType [arraySize]; / / int [] nums=new int [10]
Get array length: arrays.length
Int [] nums; / / 1. Declare an array nums = new int [3]; / / 2. Create an array / / 3. Assign values to array elements nums [0] = 1: num [1] = 2: num [2] = 3: for (int num: nums) {/ / print all elements of array System.out.println (num);} 1.2, memory analysis
1.3. Three initializations of array
Static initialization
/ / static initialization: create + assign int [] a = {1 mans= 2) 3}; Man [] mans= {new Man (1), new Man (2)}
Dynamic initialization
/ / contains default initialization int [] a=new int [2]; / / default value is 0a [0] = 1tera [1] = 2
Default initialization
An array is a reference type, and its elements are equivalent to instance variables of the class, so once the array allocates space, each element in it is implicitly initialized in the same way as the instance variables.
1.4. The basic characteristics of the array
Its length is fixed, and once the array is created, its size is immutable.
Its elements must be of the same type, and mixed types are not allowed.
Elements in an array can be any data type, including primitive and reference types.
Array variables are of reference type, and arrays can also be treated as objects, where each element is equivalent to a member variable of that object.
The array itself is an object, and the object in Java is in the heap, so whether the array holds the original type or other object types, the array itself is in the heap.
1.5. legal interval of array boundary subscript: [0, length-1], an error will be reported if it is out of bounds; public static void main (String [] args) {int [] a=new int [2]; system.out.println (a [2]);} ArraylndexOutOfBoundsException: array subscript out of bounds exception!
Summary:
An array is an ordered collection array of the same data type (the data type can be any type). An array is also an object.
An array element is equivalent to a member variable of an object
Fixed or immutable in the length of an array. If you cross the line, you will report: ArraylndexOutofBounds
2. The use of array 2.1.The For-Each loop int [] arrays = {1 int 2je 3je 4je 5}; / / print all array elements JDK1.5 without subscript for (int array: arrays) {System.out.println (array);} 2.2, array operation method input / / print array element public static void printArray (int [] a) {for (array I = 0; I)
< a.length; i++) { System.out.print(a[i]+" "); }}2.3、数组作返回值//反转数组public static int[] reverse(int[] arrays){ int[] result = new int[arrays.length]; //反转的操作 for (int i = 0; i < arrays.length; i++) { result[i] = arrays[arrays.length-i-1]; } return result;}3、多维数组 多维数组可以看成数组的数组,比如二维数组就是一个特殊的数组,其每一个元素都是一个一维数组。 int arr[][] = new int[3][2]; //二维数组,三行两列 int[][] array = {{1,2},{3,4},{5,6}};//打印二维数组所有元素for (int i = 0; i < array.length; i++) { //arrays.length=3 for (int j = 0; j < array[i].length; j++) { System.out.print(array[i][j]+" "); } System.out.println();}4、Arrays类 数组的工具类java.util.Arrays 由于数组对象本身并没有什么方法可以供我们使用,但API提供了一个工具类Arrays供我们使用。 Array类中的方法都是static修饰的静态方法,使用时直接使用类名进行调用,可以不用对象调用。 常用功能 给数组赋值:fill方法。 排序:sort方法,升序。 比较数组:equals方法比较数组中元素值是否相等。 查找数组元素:binarySearch对排序好的数组进行二分查找法操作。 int[] a = {1,2,3,4,9000,32145,451,21};System.out.println(a); // [I@28d93b30 (hashcode)//Arrays.toString 打印数组元素System.out.println(Arrays.toString(a)); //[1, 2, 3, 4, 9000, 32145, 451, 21]//二分法查找某值 返回下标System.out.println(Arrays.binarySearch(a, 9000)); // 4//填充Arrays.fill(a,2,4,0); //数组[a[2]~a[4])之间填充0System.out.println(Arrays.toString(a)); //[1, 2, 0, 0, 9000, 32145, 451, 21]//升序排序Arrays.sort(a);5、冒泡排序 1、冒泡排序是八大排序最出名的排序算法。 2、代码:两层循环,外层冒泡轮数,里层依次比较。 3、当我们看到嵌套循环,应该立马就可以得出这个算法的时间复杂度为O(n2)。 //冒泡排序//1.比较数组中两个相邻的元素,如果第一个数大于第二个数,交换它们位置//2.每一次比较,都会产生一个最大或最小的数字(升序为最大数)//3.下一轮则可以少一次排序//4.依次循环,直到结束public static int[] sort(int[] array){ int temp=0; //外层循环,次数length-1 for (int i = 0; i < array.length-1; i++) { //内层循环:如果第一个数大于第二个数,交换它们位置 for (int j = 0; j < array.length-1-i; j++) { if(array[j]>Array [juni1]) {temp=array [j]; array [j] = Array [j + 1]; array [juni1] = temp; flag = true;} return array;} public static void main (String [] args) {int [] a = {8lt. System.out.println (Arrays.toString (sort)); / / [- 2,1,8,19,35,47]}
Optimize
/ / Bubble sorting algorithm int [] a = {6 int, 2, 4, 5, 2, 1, 3}; int temp = 0; for (int I = 0
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.