二维数组单行排序

具体代码如下:

package com.www.a;
import java.util.*;
import java.util.Random;
public class DuiErWeiMeiHangPaiXu {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s=new Scanner(System.in);
		
		System.out.println("请输放数组的行数:");

		int row=s.nextInt();
		System.out.println("请输入数的列数:");
		int col=s.nextInt();
//定义二维并赋值	
		int [][]arr=new int[row][col];
		
		for(int x=0;x<arr.length;x++)
		{
			for(int y=0;y<arr[x].length;y++)
			{
				System.out.print("第"+(x+1)+"行第"+(y+1)+"列的数为:");
				arr[x][y]=s.nextInt();
			}
		}
		
		
		
		for(int x=0;x<arr.length;x++)
		{
			for(int y=0;y<arr[x].length;y++)
			{
				System.out.print(arr[x][y]+" ");
			}
			System.out.println();
		}
		
		for(int x=0;x<arr.length;x++)
		{
			for(int y=0;y<arr[x].length;y++)
			{
				for(int z=y;z<arr[x].length-1;z++)
				{
					if(arr[x][y]>arr[x][z+1])
					{
						int temp=arr[x][y];
						arr[x][y]=arr[x][z+1];
						arr[x][z+1]=temp;
					}
				}
			}
		}
		System.out.println("排完序的数组为:");
		for(int x=0;x<arr.length;x++)
		{
			for(int y=0;y<arr[x].length;y++)
			{
				System.out.print(arr[x][y]+" ");
			}
			System.out.println();
		}
	}

}

猜你喜欢

转载自xiaojingjing.iteye.com/blog/2267159