目录
编辑1.5将数组传递给方法和从方法返回数组Passing and returning arrays to/from methods
1.5.1将数组传递给方法Passing Arrays to Methods
1.5.2从方法中返回数组Returning an Array from a Method
1.数组
- 定义:数组是一种数据结构,它表示一组相同类型的数据集合。
An array is a data structure that represents a collection of the same type of data
![]()

1.1数组的变量
- 声明数组变量
·elementDatatype[ ] arrayRefVar ;
eg : double[ ] myList ;
·elementDatatype arrayRefVar [ ] ; 在java中允许,但不推荐
eg: double myList [ ] ;
- 索引变量
·数组元素通过索引访问。The array elements are accessed through the index.
>数组的索引是基于0的,也就是说,它从0开始,到arrayRefVar.length - 1结束。
The array indices are 0-based, i.e., it starts from 0 to arrayRefVar.length - 1.
>数组中的每个元素使用以下语法表示,这种语法被称为索引变量:
arrayRefVar[index]
Each element in the array is represented using the following syntax, known as an indexed variable: arrayRefVar[index]
·使用索引变量 Using Indexed Variables
>数组创建后,索引变量可以像普通变量一样使用 After an array is created, an indexed variable can be used in the same way as a regular variable.
>myList[0]引用数组中的第一个元素myList[0] references the first element in the array
>myList[9] 引用数组的最后一个元素myList[9] references the last element in the array
·示例:
myList[0] = 1;
myList[1] = 2;
myList[2] = myList[0] + myList[1];
1.2数组操作
- 创建数组
·arrayRefVar = new elementDatatype[arraySize] ;
eg: mylist = new double[10] ;
- 同时声明和创建数组:
·elementDatatype[ ] arrayRefVar = new elementDatatype[ arraySize ] ;
eg: double[ ] myList = new double[10] ;
- 数组初始化
·同时进行声明、创建、初始化数组:
double[] myList = {1.9, 2.9, 3.4, 3.5};
·这种简写语法必须在一个语句中完成! 这种简写表示法等同于以下语句:This shorthand syntax must be in one statement! This shorthand notation is equivalent to the following statements:
·其他语法:
>double[ ] myList = new double[ ] {1.9,2.9,3.4,3.5} ;
>double[ ] myList2 ;
myList2 = new double[ ]{1.9,2.9,3.4,3.5} ;
- 遍历数组:增强for循环(for each 循环)
·for each 循环能够不使用索引变量就能顺序遍历整个数组。
·一般来说,语法如下:
for (elementType value : arrayRefVar) {
// 处理值}
>注意:如果你希望以不同的顺序遍历数组或更改数组中的元素,你仍需要使用索引变量Note: You still have to use an index variable if you wish to traverse the array in a different order or change the elements in the array.
1.3数组性质
- 数组的长度
·一旦创建了数组,其大小就固定了。Once an array is created, its size is fixed.
·它不能被改变。It cannot be changed.
·你可以通过arrayRefVar.length来获取数组的大小 You can find the array size using: arrayRefVar.length
- 数组元素的默认值
·数值型基本数据类型的默认值是0 0 for the numeric primitive data types
·char类型的默认值是'\u0000' '\u0000' for char types
·boolean类型的默认值是false, false for boolean types
·引用类型的默认值是null。null for reference types
1.4数组的常见算法
- 使用输入值初始化数组 Initializing arrays with input values

- 使用随机数初始化 Initializing arrays with random values

- 打印数组 Printing arrays

- 求和 Summing all elements

- 最大值
- 最小值
- 随机打乱数组(洗牌)Random shuffling


- 左移位Shifting Left

- 右位移 Shifting Right
- 复制
·不能使用“=”来复制!You don’t copy with “=“ !

·使用循环

·复制工具 System.arraycopy(源数组, 源起始位置, 目标数组, 目标起始位置, 长度);
1.5将数组传递给方法和从方法返回数组Passing and returning arrays to/from methods
1.5.1将数组传递给方法Passing Arrays to Methods
- 实现;
- 调用方法Invoke the method

![]()
- 匿名数组 Anonymous Array
·调用printArray( );方法时,使用以下语法创建数组:
new dataType[]{literal0, literal1, ..., literalk}
·这个数组没有显式的引用变量。 这种数组被称为匿名数组。 here is no explicit reference variable for the array. Such array is called an anonymous array.
printArray(new int[]{3, 1, 2, 6, 4, 2});
- 按值传递 - Java 使用按值传递的方式来向方法传递参数。
Pass By Value - ava uses pass by value to pass arguments to a method.
·对于数组类型的参数,参数的值包含对数组的引用,这个引用被传递给方法。For a parameter of an array type, the value of the parameter contains a reference to an array; this reference is passed to the method.
>在方法体内对数组所做的任何更改都会影响到作为参数传递的原始数组。
Any changes to the array that occur inside the method body will affect the original array that was passed as the argument.
·对于基本类型值的参数,基本类型值是实际值被传递。
Different from a parameter of a primitive type value where the actual value is passed.
>在方法内部更改局部参数的值不会影响方法外部变量的值。
Changing the value of the local parameter inside the method does not affect the value of the variable outside the method.
1.5.2从方法中返回数组Returning an Array from a Method
- 反转数组
·使用不同数组
·使用相同数组

1.6搜索数组 Searching Arrays
- 搜索是在数组中查找特定元素的过程 Searching is the process of looking for a specific element in an array
1.6.1线性搜索
- 方法:public static int linearSearch(int[] list, int key)
·返回数组list中key第一次出现的索引, 如果没有找到,则返回-1。
return the index of the first occurrence of the key in list. if not found, return -1

1.6.2二分搜索 Binary Search
- 如果一个数组已经是有序的,那么查找一个元素会更加高效。
If an array is already ordered, then it is cheaper to find an element
- 二分搜索首先将键值数组中间的元素进行比较,考虑以下三种情况(假设升序):
The binary search first compares the key with the element in the middle of the array.
·如果键值小于中间元素,你只需要在数组的前半部分搜索键值 If the key is less than the middle element, you only need to search the key in the first half of the array.
·如果键值等于中间元素,搜索结束并匹配成功 If the key is equal to the middle element, the search ends with a match.
·如果键值大于中间元素,你只需要在数组的后半部分搜索键值 If the key is greater than the middle element, you only need to search the key in the second half of the array.
- 方法:public static int binarySearch(int[] list, int key)

- Arrays.binarySearch方法
·Java在java.util.Arrays类中提供了几个重载的binarySearch方法,用于在整型、双精度浮点型、字符型、短整型、长整型和单精度浮点型数组中搜索一个键值 Java provides several overloaded binarySearch methods for searching a key in an array of int, double, char, short, long, and float in the java.util.Arrays class.
·int[] list = {1, 2, 3, 4, 6, 7, 8, 9};
System.out.println("Index is " + java.util.Arrays.binarySearch(list, 6));
1.7排序数组
1.7.1选择排序Selection sort
- 首先找到列表中的最小数字并将其放在第一位。然后,它在剩余的列表中找到最小的数字并将其放在第二位,依此类推,直到列表中只剩下一个数字。finds the smallest number in the list and places it first. It then finds the smallest number in the remaining list and places it second, and so on until the list contains only a single number.
- 代码实现
1.7.2插入排序 insertion sort
- 算法通过重复将一个未排序的元素插入到已排序的子列表中,直到整个列表被排序。
The algorithm sorts a list of values by repeatedly inserting an unsorted element into a sorted sublist until the whole list is sorted.
- 代码实现

- Arrays.sort方法The Arrays.sort Method
·由于排序在编程中经常使用,Java在java.util.Arrays类中提供了几个重载的排序方法,用于对整型、双精度浮点型、字符型、短整型、长整型和单精度浮点型数组进行排序。
Since sorting is frequently used in programming, Java provides several overloaded sort methods for sorting an array of int, double, char, short, long, and float in the java.util.Arrays class.
1839

被折叠的 条评论
为什么被折叠?



