Java深入浅出数组的定义与使用上篇
作者:K媾? 时间:2022-03-10 22:32:58
一、数组的基本用法
1.什么是数组
数组:存储一组相同数据类型的数据的集合。
2.定义数组
int[] :int类型数组
double[] :double类型数组
通过类型就可以定义变量,比如:
int[] array,array就是这个类型的变量,这个变量是存储一组相同数据的变量。
三种数组定义的方式:
第一种:
int[] array = {1,2,3,4,5,6};
定义一个数组且初始化
虽然没写new,但实际上也是一个对象
注意事项:
int[10] array = {1,2,3,4,5,6};
写法错误,int[] 属于类型,中括号里面不能加任何的数字,相当于在这里破坏了它的类型。
第二种:
int[] array2 = new int[3];
定义数组未初始化
第三种:
int[] array3 = new int[]{1,2,3,4,5,6};
定义且初始化
三种当中用的最多的就是第一种
3.数组的使用
获取数组长度:
Java里面可以直接通过数组名array.length 求出当前数组的长度
访问数组元素:
访问数组下标为4的元素
越界访问数组元素:
Java语言越界直接报错
更改数组元素:
通过数组名中括号的方式,不仅仅可以访问下标的内容,也可以往下标里面写一些数据
打印数组:
第一种:(for循环)
第二种:(增强for循环,for each循环)
for循环和for each循环的区别:
for循环是可以拿到下标的,
for each循环是拿不到下标的,更多的是用于集合中
第三种:借助Java的操作数组工具类 Arrays
二、数组作为方法的参数
基本用法
JVM简单介绍
局部变量在内存中的储存:
引用不指向对象写法:
这个引用不指向任何的对象
一个引用能不能同时指向多个对象吗?
对于这个代码来说只能指向一个对象,存一个对象的地址。最终只保存了最后一个对象的地址
数组作为方法的参数传递的过程:
求解打印结果:
前两种求解:
fun2打印结果:
分析例子:下图代表什么
代表array2这个引用,指向了array1这个引用指向的对象。
下图就代表了上面例子的意思:
注意事项:
引用指向引用这句话 是错误的,引用只能指向对象
引用一定在栈上吗?
不一定,一个变量在不在栈上,是你变量的性质决定的,如果你就是一个局部变量,一定是在栈上的。如果不是,例如,实例成员变量那就不一定就是在栈上的。
三、数组练习题
1.交换两个变量的值
public class TestDemo {
public static void swap(int[] array){
int tmp = array[0];
array[0] = array[1];
array[1] = tmp;
}
public static void main(String[] args) {
int[] array = {10,20};
System.out.println("交换前: "+array[0]+" "+array[1]);
swap(array);
System.out.println("交换后: "+array[0]+" "+array[1]);
}
打印结果:
2.写一个方法, 将数组中的每个元素都 * 2
/**
* 在原来的数组上扩大2倍
* @param array
*/
public static void enlarge(int[] array){
for (int i = 0; i <array.length ; i++) {
array[i] = array[i]*2;
}
}
public static void main(String[] args) {
int[] array = {1,2,3,4,5,6,7};
enlarge(array);
System.out.println(Arrays.toString(array));
}
打印结果:
把原来数组扩大2倍的值放在一个新的数组中
/**
* 把原来数组扩大2倍的值放在一个新的数组中
* @param array
* @return
*/
public static int[] func(int[] array) {
int[] ret = new int[array.length];
for (int i = 0; i < array.length; i++) {
ret[i] = array[i] * 2;
}
return ret;
}
public static void main(String[] args) {
int[] array = {1,2,3,4,5,6,7};
int[] ret = func(array);
System.out.println(Arrays.toString(ret));
}
3.模拟实现tostring函数
public static String myToString(int[] array){
String str = "[";
for (int i = 0; i <array.length ; i++) {
str = str+array[i];
if(i != array.length-1){
str+= ",";
}
}
str= str + "]";
return str;
}
public static void main(String[] args) {
int[] array = {1,2,3,4,5,6,7};
String str = myToString(array);
System.out.println(str);
}
打印结果:
4.找数组中的最大元素
public static int maxNum(int[] array){
if(array == null) return -1;
if (array.length == 0) return -1;
int max = array[0];
for (int i = 1; i <array.length ; i++) {
if(max < array[i]){
max = array[i];
}
}
return max;
}
public static void main(String[] args) {
int[] array = {12,8,14,26,5,7,8};
int max = maxNum(array);
System.out.println(max);
}
打印结果:
5.查找数组中指定元素(顺序查找)
public static int findNum(int[] array,int key){
for (int i = 0; i <array.length ; i++) {
if(array[i] == key){
return i;
}
}
return -1;
}
public static void main(String[] args) {
int[] array = {2,4,5,6,11,7,8,9};
System.out.println(findNum(array, 7));
}
打印结果:
6.查找数组中指定元素(二分查找)
二分查找的必要条件是必须有序的数列
public static int binarySearch(int[] array,int key){
int left = 0;
int right = array.length-1;
while(left <= right){
int mid = (left+right)/2;
if(array[mid] > key){
right = mid - 1;
}else if(array[mid] < key){
left = left + 1;
}else{
return mid;
}
}
return -1;
}
public static void main(String[] args) {
int[] array = {12,14,15,16,18,23};
System.out.println(binarySearch(array, 15));
}
打印结果:
总结:
本文简单介绍了什么是数组、数组的写法、如何打印数组、数组的简单使用。通过简单例题的方式加深对数组的理解。上述就是今天的内容,文章哪里出现了问题我都会积极改正,也希望大家能更快的掌握自己想要的知识,让我们一起加油!!!!!
来源:https://blog.csdn.net/m0_64397675/article/details/123271586
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java京东面试题之为什么HashMap线程不安全
![](https://img.aspxhome.com/file/2023/5/65615_0s.png)
java生成图片验证码实例代码
java编程基础之模仿用户登录代码分享
JavaWeb Servlet实现文件上传与下载功能实例
![](https://img.aspxhome.com/file/2023/2/62532_0s.png)
idea maven pom不自动更新的解决方法
![](https://img.aspxhome.com/file/2023/9/62209_0s.jpg)
java int类型二维数组实现“杨辉三角”的完整实例
![](https://img.aspxhome.com/file/2023/7/64757_0s.png)
Java实现根据模板自动生成新的PPT
![](https://img.aspxhome.com/file/2023/7/65797_0s.jpg)
Java日期与时间类原理解析
详解SpringBoot注解读取配置文件的方式
FeignClient如何脱离eureka自定义URL
Java continue break制作简单聊天室程序
Spring自动配置之condition条件判断上篇
![](https://img.aspxhome.com/file/2023/5/62055_0s.png)
Java 10 局部变量类型推断浅析
RocketMQ4.5.X 实现修改生产者消费者日志保存路径
![](https://img.aspxhome.com/file/2023/6/62076_0s.png)
java中文转全拼工具类分享
Java 字符串反转实现代码
SpringMVC @RequestBody自动转json Http415错误的解决
![](https://img.aspxhome.com/file/2023/0/63070_0s.jpg)
Java Callable接口实现细节详解
![](https://img.aspxhome.com/file/2023/5/58995_0s.png)
基于Map的computeIfAbsent的使用场景和使用方式
![](https://img.aspxhome.com/file/2023/6/63066_0s.png)