Java中数组的定义和使用教程(三)

作者:mrbacker 时间:2023-08-26 20:03:16 

数组排序

在很多的面试题上都会出现数组排序的操作形式。但是这个时候你千万别写上:java.util.Arrays.sort(数组)。而这种排序都是以升序为主。

基础的排序操作:

范例: 冒泡排序


public class ArrayDemo {
public static void main(String args[]) {
int data[] = new int[] {9, 3, 1, 5, 4, 2, 7, 8, 6, 0};
sort(data);
printArray(data);
}

public static void sort(int arr[]) { //实现数组排序
for(int x = 0; x < arr.length - 1; x++) {
for(int y = 0; y < arr.length - x - 1; y++) {
if(arr[y] > arr[y+1]) {
int temp = arr[y];
arr[y] = arr[y+1];
arr[y+1] = temp;
}
}
}
}
//定义一个专门进行数组输出的方法
public static void printArray(int temp[]) {
for (int i = 0; i < temp.length; i++) {
System.out.print(temp[i] + "、");
}
System.out.println();
}
}

数组转置

所谓的转置最简单的理解就是首尾交换。而如果要想实现这样的交换有两种实现思路。

思路一:开辟一个新的等长的数组,而后将原始数组倒序保存进去;


public class ArrayDemo {
public static void main(String args[]) {
int data[] = new int[] {9, 3, 1, 5, 4, 2, 7, 8, 6, 0};
data = reverse(data); //反转
printArray(data);
}
public static int [] reverse(int arr[]) {
int temp[] = new int[arr.length];
int foot = 0;
for(int x = arr.length - 1; x >= 0; x--) {
temp[foot++] = arr[x];
}
return temp;
}
//定义一个专门进行数组输出的方法
public static void printArray(int temp[]) {
for (int i = 0; i < temp.length; i++) {
System.out.print(temp[i] + "、");
}
System.out.println();
}
}

使用此类模式实现的最大问题在于开辟了两块相同的堆内存空间,所以造成空间浪费。

思路二:在一个数组上完成转换


public class ArrayDemo {
public static void main(String args[]) {
int data[] = new int[] {9, 3, 1, 5, 4, 2, 7, 8, 6, 0};
reverse(data); //反转
printArray(data);
}

public static void reverse(int arr[]) {
int center = arr.length / 2; //转换次数
int head = 0; //头部开始索引
int tail = arr.length - 1; //尾部开始索引
for(int x = 0; x < center; x++) {
int temp = arr[head];
arr[head] = arr[tail];
arr[tail] = temp;
head++;
tail--;
}
}
//定义一个专门进行数组输出的方法
public static void printArray(int temp[]) {
for (int i = 0; i < temp.length; i++) {
System.out.print(temp[i] + "、");
}
System.out.println();
}
}

这种转换只需要根据数组长度 ÷ 2即可。

如果要进行二维数组的原地转置,那么肯定有一个前提:行列要相等。

范例: 保证中间轴不变(x = y)


public class ArrayDemo {
public static void main(String args[]) {
int data[][] = new int[][] {{1, 2, 3}, {4, 5, 6},{7, 8, 9}};
reverse(data); //反转
printArray(data);
}

public static void reverse(int arr[][]) {
for(int x = 0; x < arr.length; x++) {
for(int y = x; y < arr[x].length; y++) {
if(x != y) {
int temp = arr[x][y];
arr[x][y] = arr[y][x];
arr[y][x] = temp;
}
}
}
}
//定义一个专门进行数组输出的方法
public static void printArray(int temp[][]) {
for (int i = 0; i < temp.length; i++) {
for(int j = 0; j < temp[i].length; j++) {
System.out.print(temp[i][j] + "、");
}
System.out.println();
}
System.out.println();
}
}

二分查找法

如果现在要求在一个指定的数组之中查询一个数据的位置,那么现在可能想到的最简化的实现,整体数组遍历。

范例: 顺序查找


public class ArrayDemo {
public static void main(String args[]) {
int data[] = new int[] {1, 2, 3, 4, 5, 6, 7, 8};
int search = 7;
System.out.println(index(data, search));
}
public static int index(int arr[], int key) {
for(int x = 0; x < arr.length; x++) {
if(arr[x] == key)
return x;
}
return -1;
}
}

这个的时间复杂度是n,也就是说所有的数组中的数据都需要进行一次遍历,这样才能确认所需要查找的数据是否存在,那么现在如果想进行更快速地查找,最好的做法是进行二分查找(折半查找)。

范例: 实现二分查找(采用递归)


public class ArrayDemo {
public static void main(String args[]) {
int data[] = new int[] {1, 2, 3, 4, 5, 6, 7, 8};
int search = 7;
System.out.println(index(data, search));
}
public static int binarySearch(int arr[], int from, int to, int key) {
if(from < to) {
int mid = from / 2 + to / 2; //确定中间点
if(arr[mid] = key) { //数据找到了
return mid; // 取得当前索引
}else if(key < arr[mid]) {
return binarySearch(arr, from, mid - 1; key);
}
else(key > arr[mid]){
return binarySearch(arr, mid + 1, to, key);
}
}
return -1;
}
}

但是这些都是属于数据结构课程的范围,是逻辑思维训练。

对象数组(核心)

在之前所定义的数组都属于基本数据类型数组,那么对象也可以将其定义为数组,这样的操作形式称为对象数组。对象数组往往是以引用数据类型为主的定义,例如:类、接口,而且对象数组也分为两种定义格式。

  • 对象数组动态初始化:类名称 对象数组名称[] = new 类名称[长度];

  • 对象数组静态初始化:类名称 对象数组名称[] = new 类名称[]{实例化对象,…};

范例: 对象数组的动态初始化


class Person {
private String name;
private int age;

public Person(String n, int a) {
name = n;
age = a;
}
public String getInfo() {
return "姓名:" + name + ",年龄:" + age;
}
}
public class ArrayDemo {
// 动态初始化之后对象数组中的每一个元素都是其对象数据类型的默认值
public static void main(String args[]) {
Person per[] = new Person[3]; //动态初始化
per[0] = new Person("张三", 1);
per[1] = new Person("王五", 2);
per[2] = new Person("李四", 4);
for(int x = 0; x < per.length; x++) {
System.out.println(per[x].getInfo());
}
}
}

范例: 静态初始化


class Person {
private String name;
private int age;

public Person(String n, int a) {
name = n;
age = a;
}
public String getInfo() {
return "姓名:" + name + ",年龄:" + age;
}
}
public class ArrayDemo {
// 动态初始化之后对象数组中的每一个元素都是其对象数据类型的默认值
public static void main(String args[]) {
Person per[] = new Person[] {
new Person("张三", 1),
new Person("王五", 2),
new Person("李四", 4)
}; //动态初始化
for(int x = 0; x < per.length; x++) {
System.out.println(per[x].getInfo());
}
}
}

每一个对象可以保存更多的的属性,所以对象数组保存的内容要比基本数据类型更多。那么应用的也就更多。所有的开发必定都存在有对象数组的概念。

总结

来源:https://blog.csdn.net/mrbacker/article/details/82319021

标签:java,数组,定义
0
投稿

猜你喜欢

  • SpringBoot Jpa分页查询配置方式解析

    2023-03-02 10:04:02
  • Android自定义View实现绘制虚线的方法详解

    2022-06-24 01:18:10
  • C# 中const,readonly,static的使用小结

    2022-05-16 20:39:58
  • Android实现页面短信验证功能

    2022-02-13 01:36:56
  • Springboot WebJar打包及使用实现流程解析

    2023-06-21 22:08:00
  • spring-boot-maven-plugin:打包时排除provided依赖问题

    2023-07-18 02:12:11
  • C#读写操作app.config中的数据应用介绍

    2021-07-25 21:33:19
  • Android实现轮播图片效果

    2023-07-08 02:20:24
  • Spring使用注解存储和读取对象详解

    2022-06-10 00:35:24
  • C#常用数据结构和算法总结

    2021-10-31 22:05:29
  • 详解C语言结构体,枚举,联合体的使用

    2021-10-12 09:37:01
  • java中request常用方法小结

    2022-09-13 06:19:45
  • 详解spring cloud中使用Ribbon实现客户端的软负载均衡

    2022-10-26 15:54:45
  • Kotlin数据容器深入讲解

    2022-03-28 05:19:34
  • Android 监听屏幕是否锁屏的实例代码

    2022-01-19 14:31:24
  • Spring Boot用户注册验证的实现全过程记录

    2023-01-03 01:58:34
  • java导出数据库的全部表到excel

    2023-11-25 09:38:33
  • 深入讲解基于JDK的动态代理机制

    2022-07-19 07:13:32
  • Android在JNI中使用ByteBuffer的方法

    2021-11-08 21:14:35
  • Java构建JDBC应用程序的实例操作

    2023-08-07 12:09:13
  • asp之家 软件编程 m.aspxhome.com