1、数组:
测试代码1:数组静态初始化和动态初始化
package test.arrays.com;
public class ArrayInitializationExample {
public static void main(String[] args) {
// 动态初始化数组
int[] dynamicArray = new int[5]; // 创建长度为5的整型数组
dynamicArray[0] = 1;
dynamicArray[1] = 2;
dynamicArray[2] = 3;
dynamicArray[3] = 4;
dynamicArray[4] = 5;
// 打印动态初始化数组
for (int i = 0; i < dynamicArray.length; i++) {
System.out.print(dynamicArray[i] + " ");
}
System.out.println();
// 静态初始化数组
int[] staticArray = {1, 2, 3, 4, 5}; // 直接为数组元素赋值
// 打印静态初始化数组
for (int i = 0; i < staticArray.length; i++) {
System.out.print(staticArray[i] + " ");
}
System.out.println();
}
}
测试代码2:找出数组中的最大值和最小值
package test.arrays.com;
//初始化一个整型数组arr,然后利用for循环遍历数组,
//分别找到数组中的最大值和最小值,最后打印输出。
public class ArrayMaxMin {
public static void main(String[] args) {
int[] arr = {10, 5, 7, 12, 8, 18, 1, 0, 31};
int max = arr[0];
int min = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
System.out.println("最大值:" + max);
System.out.println("最小值:" + min);
}
}
运行结果如下:
测试代码3:遍历数组
package test.arrays.com;
public class IterateArray {
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4, 5 };
//遍历一维数组
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
int[][] multiArray = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
//遍历二维数组
for (int i = 0; i < multiArray.length; i++) {
for (int j = 0; j < multiArray[i].length; j++) {
System.out.print(multiArray[i][j] + " ");
}
System.out.println();
}
//使用foreach遍历数组
for (int num : arr) {
System.out.println(num);
}
}
}
2、堆栈:
测试代码1:堆内存和栈内存
package test.arrays.com;
//变量a、str、obj分别存在于栈内存中,而String对象实例以及变量b,则分配在堆内存和栈内存中。
public class MemoryExample {
public static void main(String[] args) {
// 在栈内存中分配基本数据类型变量
int a = 5;
// 在堆内存中分配对象实例
String str = new String("Hello");
// 在栈内存中分配对象引用变量
MemoryExample obj = new MemoryExample();
// 调用方法,注意方法调用会涉及栈内存的使用
obj.method();
}
// 方法也会在栈内存中分配
public void method() {
// 在栈内存中分配局部变量
int b = 10;
}
}
测试代码2:递归调用
package test.arrays.com;
//每次递归调用factorial方法时,都会在栈内存中创建一个新的栈帧来保存方法的局部量和运行时数据。
//当递归调用结束时,对应的栈帧会被弹出,释放相关的栈内存空间。
//堆内存和栈内存协同工作,实现Java程序对对象和数据的动态管理。
//内存由虚拟机自动管理。
public class MemoryTest {
public static void main(String[] args) {
int result = factorial(5);
System.out.println("Factorial of 5 is: " + result);
}
public static int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1); // 递归调用
}
}
}
测试代码3:堆内存和栈内存
package test.arrays.com;
public class HeapAndStackExample {
public static void main(String[] args) {
// 局部变量arrayRef,它存储的是对数组的引用,存储在栈内存中
int[] arrayRef = new int[5]; // 在堆内存中创建了一个长度为5的整型数组
// 通过arrayRef引用,向堆内存中的数组赋值
for (int i = 0; i < arrayRef.length; i++) {
arrayRef[i] = i * 2; // 数组元素存储在堆内存中
}
// 访问堆内存中的数组元素
for (int i = 0; i < arrayRef.length; i++) {
System.out.println(arrayRef[i]); // 输出堆内存中的数组元素
}
// 当main方法执行完毕后,arrayRef引用(存储在栈内存中)会被自动移除
// 但堆内存中的数组对象在没有任何引用指向它时,才会被垃圾回收器回收
}
}
测试代码4:堆内存和栈内存
package test.arrays.com;
public class TestArray {
// 静态方法,用于表示数组的使用
public static void manipulateArray(int[] array) {
// array是一个局部变量,存储在栈内存中
// 但是它引用的数组对象存储在堆内存中
for (int i = 0; i < array.length; i++) {
array[i] = i * 2; // 修改堆内存中的数组元素
}
// 当方法结束时,array局部变量会从栈内存中弹出,但数组对象仍然保留在堆内存中(如果还有其他引用指向它)
}
public static void main(String[] args) {
// main方法的局部变量,存储在栈内存中
// 新创建的数组对象,该对象存储在堆内存中
int[] heapArray = new int[5]; // 数组对象在堆内存中创建
// 通过局部变量heapArray引用,访问并打印堆内存中的数组元素(初始化为0)
for (int i = 0; i < heapArray.length; i++) {
System.out.println("Before manipulation: " + heapArray[i]);
}
// 调用静态方法,将数组的引用传递给该方法
// 注意:这里传递的是数组的引用,而不是数组本身
// 因此,静态方法能够修改堆内存中的数组对象
manipulateArray(heapArray);
// 再次访问并打印堆内存中的数组元素,此时它们已经被修改
for (int i = 0; i < heapArray.length; i++) {
System.out.println("After manipulation: " + heapArray[i]);
}
// 当main方法执行完毕后,heapArray局部变量会从栈内存中移除
// 但由于没有其他引用指向heapArray引用的数组对象,并且该对象在堆内存中不再可达
// 因此,该数组对象会成为垃圾回收的目标
}
}
测试代码5:数组的使用,修改图片的颜色。
修改前:
修改后:
代码如下:
package test.arrays.com;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
//图像像素存取通常通过使用 BufferedImage 类实现。
//BufferedImage 类提供直接访问和操作像素数据的方法。
public class ImagePixelAccess {
public static void main(String[] args) {
try {
// 读取图像文件
File imageFile = new File("C:\\Users\\Administrator\\Desktop\\input.png");
BufferedImage image = ImageIO.read(imageFile);
// 获取图像的宽度和高度
int width = image.getWidth();
int height = image.getHeight();
// 读取和修改像素值
int[] pixel = new int[15];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// 获取像素值
image.getRaster().getPixel(x, y, pixel);
// 修改像素值
pixel[0] = 255 - pixel[0]; // 修改红色分量
pixel[1] = 255 - pixel[1]; // 修改绿色分量
pixel[2] = 255 - pixel[2]; // 修改蓝色分量
// 设置修改后的像素值
image.getRaster().setPixel(x, y, pixel);
}
}
// 将修改后的图像保存到文件
File outputImageFile = new File("C:\\Users\\Administrator\\Desktop\\output.png");
ImageIO.write(image, "png", outputImageFile);
System.out.println("图像处理完成");
} catch (IOException e) {
e.printStackTrace();
}
}
}