---
title: "黑马程序员—数组(反转)"
author: "wuqiwei"
date: "2013-02-05T22:46:00Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2013-02-05T22:54:19Z"
id: 1796018
url: "https://wuqiwei.iteye.com/blog/1796018"
---

------- [android培训](http://www.itheima.com)、[java培训](http://www.itheima.com)、期待与您交流！ ----------

![](http://dl.iteye.com/upload/attachment/0080/3348/1e53b233-39e9-33ee-a18a-eb39d5de1337.jpg)

 

```
class ArrayTest3// 反转
/*
对给定的数组进行反转。
{3,1,5,6,2} --->{2,6,5,1,3}
*/
{	
	public static void main(String[] args) 
	{	
		int [] arr={3,1,5,6,2};
		reverseArray(arr);
		printArray(arr);
		System.out.println( );
		System.out.println("Hello World!");
	}

	public static void reverseArray(int [] arr)
		{
			for (int start=0,end=arr.length-1 ;start<end; start++,end--)
//这里定义的start 和他们各自的增减中间都是用, for的各独立组成部分间才用;
			{
				/*int temp=arr[start];
				 arr[start]=arr[end];
				 arr[end]=temp;*/
				swap(arr,start,end);				
			}
		}
	
	public static void swap(int []arr,int a,int b)//注意是,不是;for里面才是;
		{
			int temp=arr[a];//不要写成int temp=a!!!
			 arr[a]=arr[b];
			 arr[b]=temp;
		}

	public static void printArray(int [] arr)
		{
			System.out.print("[");
			for (int x=0;x<arr.length;x++ )
			{
				if (x<arr.length-1)
				{
					System.out.print(arr[x]+",");
				}
				else
					System.out.print(arr[x]+"]");
			}
		}

}
```

   
![](http://dl.iteye.com/upload/attachment/0080/3350/d90a0321-bdad-3a18-b916-f38ca43c0183.jpg)