java数组价值_JAVA的数组

1. 数组的定义:

1.1 存放一组数据类型相同的变量;

1.2 声明/定义/初始化:元素数据类型[] 数组名 = new 元素数据类型[数组长度];

1.3 数组名存放在栈中,数组本身是一个对象,数组对象本身存放在堆上。

1.3.1 如果只声明数组名时(int[] nums;),栈中存放了数组名,但是堆中没有数组对象;

1.3.2 只有实例化数组对象时(nums = {1, 2, 3}; nums = new int[5];),堆上开辟空间存放数组对象,数组名指向数组对象。

c73208ccd954dda97e01ca2d0b39746c.png

package com.langtao.array;

public class Demo2 {

public static void main(String[] args) {

int[] numbers = new int[10];

for (int i = 0; i < numbers.length; i++) {

numbers[i] = i + 1;

}

for (int i = 0; i < numbers.length; i++) {

System.out.println("numbers[" + i +"]=" +numbers[i]);

}

int[] array1 = {1, 2, 3};

Demo1[] d1 = {new Demo1(), new Demo1()};

}

}

2. 数组的使用

package com.langtao.array;

public class Demo3 {

public static void main(String[] args) {

int[] arrayOriginal = {1, 2, 3, 4, 5};

printArray(arrayOriginal);

System.out.println("==============");

int[] reverseOriginal = reverseArray(arrayOriginal);

printArray(reverseOriginal);

System.out.println("====In the main method call the reverse method====");

reverseArray1(arrayOriginal);

for (int i : arrayOriginal) {

System.out.println(i);

}

System.out.println("====In the main method call the reverse method====");

}

public static void printArray(int[] array){

for (int i = 0; i < array.length; i++) {

System.out.print(array[i] + " ");

}

}

public static int[] reverseArray(int[] array){

int[] reverseArray = new int[array.length];

for (int i = 0; i < reverseArray.length; i++) {

reverseArray[i] = array[reverseArray.length - i - 1];

}

return reverseArray;

}

public static void reverseArray1(int[] array){

int tmp = 0;

for (int i = 0; i < array.length/2; i++) {

tmp = array[i];

array[i] = array[array.length - i - 1];

array[array.length - i - 1] = tmp;

}

System.out.println("====In the method====");

for (int i : array) {

System.out.println(i);

}

System.out.println("====In the method====");

}

}

3.二维数组

package com.langtao.array;

public class Demo4 {

public static void main(String[] args) {

int[][] array2 = {{1,2}, {2,3}, {3,4}, {4,5}};

printArray(array2[0]);

System.out.println("==============");

printArray(array2);

}

public static void printArray(int[] array){

for (int i = 0; i < array.length; i++) {

System.out.print(array[i] + " ");

}

}

public static void printArray(int[][] array){

for (int i = 0; i < array.length; i++) {

for (int j = 0; j < array[i].length; j++) {

System.out.print(array[i][j] + " ");

}

System.out.println();

}

}

}

4. 冒泡排序

package com.langtao.array;

import java.util.Arrays;

public class Demo5 {

public static void main(String[] args) {

int[] a = {1, 3, 2, 5, -1, 23, 6};

int[] b = {9, 8, 7, 6, 5, 4, 3, 2, 1};

System.out.println(Arrays.toString(a));

// Arrays.sort(a);

// System.out.println(Arrays.toString(a));

sort1(a);

System.out.println(Arrays.toString(a));

sort1(b);

System.out.println(Arrays.toString(b));

}

public static void sort1(int[] array){

for (int i = 0; i < array.length - 1 ; i++) {

for (int j = 0; j < array.length - 1 - i; j++) {

if (array[j] > array[j + 1]){

int tmp = array[j];

array[j] = array[j + 1];

array[j + 1] = tmp;

}

}

}

}

}

5.稀疏数组

package com.langtao.array;

import java.util.Arrays;

/*

* The sparse array is used to compress the array which has only limited unequal values comparing to the majority ones.

* For example, the original array can be like this:

* 0 0 0 0 0

* 0 1 0 0 0

* 0 0 2 0 0

* 0 0 0 0 0

* 0 0 0 0 0

* 0 0 0 0 0

* We can compress this array into a sparse array as:

* 6 5 2

* 1 1 1

* 2 2 2

* In the first line of the array, we record the size and non-zero elements. Then in the second row, we record the first

* non-zero element's coordinates and value and in the next lines we record the same information.

* Then you can transform the sparse array back to the original array.

* */

public class Demo6 {

public static void main(String[] args) {

int[][] array = new int[6][5];

array[1][2] = 1;

array[2][3] = 2;

array[1][1] = 99;

printArray(array);

System.out.println("================");

int[][] sparseArray = sparseArray(array);

printArray(sparseArray);

System.out.println("****************");

int[][] originalArray = reverseSparseArray(sparseArray);

printArray(originalArray);

}

public static void printArray(int[][] array){

for (int i = 0; i < array.length; i++) {

for (int j = 0; j < array[i].length; j++) {

System.out.print(array[i][j] + " ");

}

System.out.println();

}

}

public static int[][] sparseArray(int[][] array){

//get the numbers of non-zero values in the original array.

int count = 0;

for (int i = 0; i < array.length; i++) {

for (int j = 0; j < array[i].length; j++) {

if(array[i][j] != 0){

count++;

}

}

}

/*

In the first row of the sparse array, it records the size of the original array and the

total number of the non-zero elements.

*/

int[][] sparseArray = new int[count + 1][3];

sparseArray[0][0] = array.length;

sparseArray[0][1] = array[0].length;

sparseArray[0][2] = count;

int index = 1;

for (int i = 0; i < array.length; i++) {

for (int j = 0; j < array[i].length; j++) {

if (array[i][j] != 0){

sparseArray[index][0] = i;

sparseArray[index][1] = j;

sparseArray[index][2] = array[i][j];

index++;

}

}

}

return sparseArray;

}

public static int[][] reverseSparseArray(int[][] array){

int row = array[0][0];

int colume = array[0][1];

int[][] reverseSparseArray = new int[row][colume];

for (int i = 1; i < array.length; i++) {

int realRow = array[i][0];

int realColume = array[i][1];

reverseSparseArray[realRow][realColume] = array[i][2];

}

return reverseSparseArray;

}

}

本项目属于机器学习的简单部分,基于为了快速理解机器学习而搭建的人工智能速成项目,大家可以根据其中的项目时间进行相关的学习.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值