• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

fairyperfect

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

数组的应用

数组的应用

遍历数组

遍历数组就是获取数组中的每个元素
遍历一维数组
for循环
遍历二维数组
1.双for循环
2.双foreach循环

代码运行

点击查看代码
public class arry6 {

	public static void main(String[] args) {
		 char arr[][]=new char [4][];
		 arr[0]=new char[]{'春','眠','不','觉','晓'};
		 arr[1]=new char[]{'处','处','闻','啼','鸟'};
		 arr[2]=new char[]{'夜','来','风','雨','声'};
		 arr[3]=new char[]{'花','落','知','多','少'};
		 for(int i=0;i<arr.length;i++){
			 for(int j=0;j<arr[i].length;j++)
			 {
				 System.out.print (arr[i][j]);
			}
	 System.out.println();	 
		 }

	}

}

代码运行

点击查看代码
public class arry6 {

	public static void main(String[] args) {
		 char arr[][]=new char [4][];
		 arr[0]=new char[]{'春','眠','不','觉','晓'};
		 arr[1]=new char[]{'处','处','闻','啼','鸟'};
		 arr[2]=new char[]{'夜','来','风','雨','声'};
		 arr[3]=new char[]{'花','落','知','多','少'};
		 for(char a[]:arr){
			 for(char b:a)
			 {
				 System.out.print(b);
			}
	 System.out.println();	 
		 }

	}

}


填充和批量替换数组元素

语法
Arrays.fill(arr,int value)
arr---数组
value---填充的值
Arrays.fill(arr,int fromIndex,int toIndex,int value)
fromIndex---填充的第一个索引(包括)
toIndex---填充的最后一个索引(不包括)

代码运行

点击查看代码
import java.util.Arrays;
public class arry2 {
      public static void main(String[] args) {
	  int arr[]=new int[5];
	  arr[0]=7;
	  Arrays.fill(arr,13);
	  for(int i=0;i<arr.length;i++){
		  System.out.println("第"+i+"个元素的值是"+arr[i]);
		  
	  }

	}

}

代码运行

点击查看代码
import java.util.Arrays;

public class arry3 {

	public static void main(String[] args) {
		 int arr[]={1,9,5,5,2,6,9,7,5,5,8};
		 Arrays.fill(arr,3,7,0);
		 for(int i=0;i<arr.length;i++){
			 if(arr[i]==0){
			 System.out.print("*");} 
			 else{
				 System.out.print (arr[i]);
			 }
		 }
	}

}



复制数组

语法
Arrays.copyOf(arr,newlength)
arr---数组
newlength---指复制后的新数组的长度
Arrays.copyOfRange(arr,formIndex,toIndex)
formIndex---指定开始复制数组的索引位置(包括)
toIndex---要复制范围的最后索引位置(不包括)

代码运行

点击查看代码
import java.util.Arrays;

public class arry4 {

	public static void main(String[] args) {
		 int arr[]={1,2,3};
		 int b[]=Arrays.copyOf(arr, 6);
         b[0]=77;
         System.out.println("arr数组");
         for(int temp:arr){
        	 System.out.print(temp+" ");
         }
         System.out.println("\n数组");
         for(int temp:b){
        	 System.out.print(temp+" ");
         }
		 
	}

}

代码运行

点击查看代码
import java.util.Arrays;

public class arry4 {

	public static void main(String[] args) {
		 int arr[]={1,2,3,4,5,6,7,8,8};
		 int b[]=Arrays.copyOfRange(arr, 2,4+1);
   
         System.out.println("arr数组");
         for(int temp:arr){
        	 System.out.print(temp+" ");
         }
         System.out.println("\n数组");
         for(int temp:b){
        	 System.out.print(temp+" ");
         }
		 
	}

}


对数组进行排序

Arrays.Sort()方法
语法 Arrays.sort(arr);
示例 int arr[]=new int[]{23,42,12,8};
Arrays.sort(arr);

代码运行

点击查看代码
import java.util.Arrays;

public class arry5 {

	public static void main(String[] args) {
		 int a[]=new int[]{24,56,3,55,5};
		 double b[]=new double[]{1.2,4.4,1.3,2.1,2.9,3.1};
		 Arrays.sort( a);
		 Arrays.sort( b);
		 for(int temp:a){
			 System.out.print (temp+" ");
		 }
		 System.out.println( );
	     for(double temp:b){
	    	 System.out.print (temp+" ");
	    	 
	     }
	} 

}
弊端利用此函数只能从小到大进行排序

posted on 2022-11-24 16:33  小废物的宝藏  阅读(42)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3