案例1:存储奇数
创建一个数组用来存储1~100以内的奇数:
这题比较简单,我们直接上代码:
//存储1~100以内的奇数
class ArrayStockOne{
public static void main(String [] args){
int[] array=new int[50]; //动态初始化创建数组
int x=0; //换行标记
for(int a=0;a<50;a++){ //循环存储
array[a]=2*a+1; //存储奇数
}
for(int count:array){ //增强for循环输出
System.out.print(count+" ");
x++;
if(x==5){
System.out.println("\n");//每行输出五个就会换行
x=0;
}
}
}
}
C:\Java\jdk-17.0.11\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\lib\idea_rt.jar=5934:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Test\baciccode\out\production\baciccode Maycode.ArrayStockOne
1 3 5 7 9
11 13 15 17 19
21 23 25 27 29
31 33 35 37 39
41 43 45 47 49
51 53 55 57 59
61 63 65 67 69
71 73 75 77 79
81 83 85 87 89
91 93 95 97 99
Process finished with exit code 0
案例2:交换对应位置的元素
交换两个数组中的元素并在控制台输出;
这是我第一次想出来的办法,有点取巧的感觉,不过适用于两个数组长度不相等的交换;
//将两个数组对应位置的元素互换
class ArrayExchange{
public static void main(String [] args){
int[] array1={11,38,44,99};
int[] array2={333,555,222,888};
int[] array=new int[array1.length]; //创建第三个数组
array=array1; /*数组名是数组的首地址,通过交换基地值进而找到数组中的其它元素*/
array1=array2;
array2=array;
for(int account:array1){ //输出数组
System.out.print(account+"\t");
}
System.out.println(" "); //换行
for(int account:array2){ //输出数组
System.out.print(account+"\t");
}
}
}
C:\Java\jdk-17.0.11\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\lib\idea_rt.jar=7255:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Test\baciccode\out\production\baciccode Maycode.ArrayExchange
333 555 222 888
11 38 44 99
Process finished with exit code 0
以下是相对容易理解且理解的方法:
//借鉴两个整型变量交换数值的思路
class ArrayExchangeTwo{
public static void main(String [] args){
int[] array1={11,33,66,87};
int[] array2={112,334,667,879};
for(int a=0;a<array1.length;a++){
int x; //交换数值
x=array1[a];
array1[a]=array2[a];
array2[a]=x;
}
for(int count:array1){ //输出数组
System.out.print(count+"\t");
}
System.out.println(" "); //换行
for(int count:array2){ //输出数组
System.out.print(count+"\t");
}
}
}
C:\Java\jdk-17.0.11\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\lib\idea_rt.jar=8793:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Test\baciccode\out\production\baciccode Maycode.ArrayExchangeTwo
112 334 667 879
11 33 66 87
Process finished with exit code 0
案例3:互换数组的头尾位置
将一个数组的头尾位置互换,如 a[4]={11,13,45,67},互换后,a[4]={67,45,13,11};
我们先给定一个偶数个的数组元素:
如下,在一开始,无论我怎么修改,我都找不到自己的错误在哪,我自己都在纳闷怎恶魔没有i奥换数据呢?:
//给定一个数组,将数组元素头尾互换
class ArrayExchangeFromTo{
public static void main(String[] args){
int[] array={11,22,33,44,55,66};
//0-4 1-3 2-2
for(int a=0;a<array.length;a++){
int x;
x=array[a];
array[a]=array[array.length-a-1];
array[array.length-a-1]=x;
}
for(int count:array){
System.out.println(count);
}
}
}
C:\Java\jdk-17.0.11\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\lib\idea_rt.jar=11032:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Test\baciccode\out\production\baciccode Maycode.ArrayExchangeFromTo
11
22
33
44
55
66
Process finished with exit code 0
原来for循环条件里是 a < array.length ,这样,会发生两次交换,也就是数值又交换回去了。于是我让她 /2 ;
//给定一个数组,将数组元素头尾互换
class ArrayExchangeFromTo{
public static void main(String[] args){
int[] array={11,22,33,44,55,66};
//0-4 1-3 2-2
for(int a=0;a<array.length/2;a++){
int x;
x=array[a];
array[a]=array[array.length-a-1];
array[array.length-a-1]=x;
}
for(int count:array){
System.out.println(count);
}
}
}
C:\Java\jdk-17.0.11\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\lib\idea_rt.jar=11336:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Test\baciccode\out\production\baciccode Maycode.ArrayExchangeFromTo
66
55
44
33
22
11
Process finished with exit code 0
对于奇数个元素的数组同样适用:
//给定一个数组,将数组元素头尾互换
class ArrayExchangeFromTo{
public static void main(String[] args){
int[] array={11,22,33,44,55};
//0-4 1-3 2-2
for(int a=0;a<array.length/2;a++){
int x;
x=array[a];
array[a]=array[array.length-a-1];
array[array.length-a-1]=x;
}
for(int count:array){
System.out.println(count);
}
}
}
C:\Java\jdk-17.0.11\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\lib\idea_rt.jar=11433:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Test\baciccode\out\production\baciccode Maycode.ArrayExchangeFromTo
55
44
33
22
11
Process finished with exit code 0
案例4:求数组中元素的平均值
给定一个数组,求其元素累加和的平均值;
class OrdinaryArray{
public static void main(String [] args){
int[] array={11,33,44,55,6,7,8,9};
int sum=0;
for(int a=0;a<array.length;a++){
sum+=array[a];
}
System.out.println(sum/array.length);
}
}
C:\Java\jdk-17.0.11\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\lib\idea_rt.jar=11936:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Test\baciccode\out\production\baciccode Maycode.OrdinaryArray
21
Process finished with exit code 0
当然,我们可以输入数组中的元素:
class OrdinaryArray{
public static void main(String [] args){
int[] array=new int[6]; //动态初始化创建数组
int sum=0; //计算累加和
System.out.println("请输入数组中的元素:");//输入数组中的元素
for(int a=0;a<array.length;a++){
Scanner scn=new Scanner(System.in);
array[a]=scn.nextInt();
}
for(int a=0;a<array.length;a++){
sum+=array[a]; //计算平均值
}
System.out.println("平均值:"+sum/array.length);
}
}
C:\Java\jdk-17.0.11\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\lib\idea_rt.jar=12660:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Test\baciccode\out\production\baciccode Maycode.OrdinaryArray
请输入数组中的元素:
22
33
44
55
66
77
平均值:49
Process finished with exit code 0
案例5:找极值
给定一个数组,找寻其中的极值(极大值,极小值);
代码如下:
// 找数组中的极大值/极小值
class ExceedCount{
public static void main(String [] args){
int[] array={11,33,44,55,66,88};
int x=0; //作为输出的标记
for(int a=0;a<array.length;a++){
int number=array[0];
if(number<=array[a]){
number=array[a]; //从小到大排
x++;
}
if(x==array.length){
System.out.println(array[array.length-1]);
//System.out.println(number); //最大值
System.out.println(array[0]); //最小值
}
}
}
}
C:\Java\jdk-17.0.11\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\lib\idea_rt.jar=4263:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Test\baciccode\out\production\baciccode Maycode.ExceedCount
88
11
Process finished with exit code 0
当然,这个思维比较混乱,再修改一下:
// 找数组中的极大值/极小值
class ExceedCountPro{
public static void main(String [] args){
int[] array={11,33,44,55,66,88};
int max=array[0]; //标记最大值
int mix=array[0]; //标记最小值
for(int a=0;a<array.length;a++){
if(max<=array[a]){
max=array[a]; //较大值赋给max
}
if(mix>=array[a]){
mix=array[a]; //较小值赋给mix
}
}
System.out.println(mix); //输出最小值
System.out.println(max); //输出最大值
}
}
C:\Java\jdk-17.0.11\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\lib\idea_rt.jar=5377:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Test\baciccode\out\production\baciccode Maycode.ExceedCountPro
11
88
Process finished with exit code 0
案例6:合并数组
给定两个数组:a {11,33,66} , b {666,888} , 将其合并为一个数组;
代码如下,主要思路是将原先的数组元素存储到新数组中;
//a {11,33,66} , b {666,888} , 将其合并为一个数组
class CombineArray{
public static void main(String[] args){
int[] array1={11,33,66};
int[] array2={666,888};
int[] array=new int[array1.length+array2.length];//动态创建新数组
for(int a=0;a<array1.length;a++){//数组一赋值到新数组中
array[a]=array1[a];
}
for(int a=0;a<array2.length;a++){//数组二复制到新数组中
array[a+array1.length]=array2[a];//这里新数组的索引不能从零开始
}
for(int count:array){ //增强 for 循环输出
System.out.println(count);
}
}
}
C:\Java\jdk-17.0.11\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\lib\idea_rt.jar=6362:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Test\baciccode\out\production\baciccode Maycode.CombineArray
11
33
66
666
888
Process finished with exit code 0
当然,也可以往新数组中存储元素:
//往新数组里添加数据
class CombineArrayTwo{
public static void main(String[] args){
int[] array1={11,33,66};
int[] array2={666,888};
int[] array=new int[array1.length+array2.length];//动态创建新数组
for(int a=0;a<array.length;a++){
if(a<array1.length){ //数组一的范围内进行赋值
array[a]=array1[a];
}
if(a>=array1.length){ //数组二的范围内进行赋值
array[a]=array2[a-array1.length];
}
}
for(int count:array){ //增强 for 循环输出
System.out.println(count);
}
}
}
C:\Java\jdk-17.0.11\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\lib\idea_rt.jar=6939:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Test\baciccode\out\production\baciccode Maycode.CombineArrayTwo
11
33
66
666
888
Process finished with exit code 0
案例7:拆分数组
给定一个数组,找寻最大值位置,并在该位置将其分为两个数组:
如 a[ ] ={11,22,33,44,99,55,66,77 } ,
分为 a1[ ] = { 11,22,33,44 } , b1[ ] = { 55 ,66 ,77 } ;
我们首先找到数组中最大元素,再找到其下标;
通过下标确定两个数组的长度,最后把元素存储在数组里;
//数组分割
class TearArray{
public static void main(String [] args){
int[] array={44,11,99,88,23,55};
int max=array[0]; //标记寻找数组最大数
int x=0; //标记寻找最大数的下标
for(int a=0;a<array.length;a++){
if(max<array[a]){
max=array[a];
x=a; //最大值下标赋给 x ;
}
}
System.out.println(max);
System.out.println("索引:"+x);
int[] array1=new int[x]; //动态创建数组1
int[] array2=new int[array.length-x-1]; //动态创建数组2
for(int a=0;a<array1.length;a++){ //数组元素存储在数组1中
array1[a]=array[a];
}
for(int a=0;a<array2.length;a++){ //数组元素存储在数组2中
array2[a]=array[(a+x)+1];
}
for(int a:array1){ //增强 for 循环输出
System.out.println(a);
}
System.out.println(" "); //增强 for 循环输出
for(int b:array2){
System.out.println(b);
}
}
}
C:\Java\jdk-17.0.11\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\lib\idea_rt.jar=11434:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Test\baciccode\out\production\baciccode Maycode.TearArray
99
索引:2
44
11
88
23
55
Process finished with exit code 0