黑马程序员—数组(反转)

------- android培训java培训、期待与您交流! ----------


 

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]+"]");
			}
		}

}

 

 

来源:网络


智能推荐

黑马程序员 十三、GUI

Java帮帮-IT资源分享网 十三、黑马程序员—GUI等 1、GUI(图形用户界面) GUI Graphical User Interface(图形用户接口)。 用图形的方式,来显示计算机操作的界面,这样更方便更直观。 CLI Command line User Interface (命令行用户接口) 就是常见的 Dos 命令行操作。 需要记忆一些常用的命令,操作不直观。 举例: 比如...

黑马程序员 十二、反射机制

Java帮帮-IT资源分享网 十二、黑马程序员—反射机制 1、反射的概念 反射的引入: Object obj = new Student(); 若程序运行时接收到外部传入的一个对象,该对象的编译类型是 Object,但程序又 需要调用该对象运行类型的方法: 1.若编译和运行类型都知道,使用 instanceof 判断后,强转。 2.编译时根本无法预知该对象属于什么类,程序只能依靠运行时...

黑马程序员 十一、网络编程

Java帮帮-IT资源分享网 十一、黑马程序员—网络编程 第 11 天:网络编程(1) 1、什么是网络编程? 网络编程的本质是两个设备之间的数据交换,当然,在计算机网络中,设备主要指计算 机。数据传递本身没有多大的难度,不就是把一个设备中的数据发送给两外一个设备,然后 接受另外一个设备反馈的数据。现在的网络编程基本上都是基于请求/响应方式的,也就是 一个设备发送请求数据给另外一个,然后...

黑马程序员 六、线程技术

Java帮帮-IT资源分享网 六、黑马程序员—线程技术 第六篇 1、进程和线程 进程是指一个内存中运行的应用程序,每个进程都有自己独立的一块内存空间,一个进程中 可以有多个线程。比如在 Windows 系统中,一个运行的 xx.exe 就是一个进程。 Java 程序的进程里有几个线程:主线程, 垃圾回收线程(后台线程) 线程是指进程中的一个执行任务(控制单元),一个进程中可以运行多个线...

黑马程序员__Java GUI编程

  day22视频 ------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ------- 1. Graphical User Interface java为GUI提供的对象都存在 java.AWT 和 javax.Swing 两个包中 java.AWT : Abstract Window Toolkit  抽象窗口工具包   ...

猜你喜欢

黑马程序员---登录进阶练习

---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ---------------------- namespace winform1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //将错误次数变为零 public vo...

黑马程序员_Java基础_入门

---------------------- ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ----------------------   1.常用DOS命令 盘符名称:进入指定的盘符,例:C:  D:  E: cd 目录名称进入/显示指定名称的目录,例:cd Itcast\day01  cd Tencent\QQ\Bin cd...

Eclipse中创建一个简单的Maven项目(详细)

前提条件:Eclipse已经整合了Maven。 简单配置Maven 已经配置好的,请跳过 配置Maven的路径: window - preferences 找到Maven展开 点击ADD   在弹出的对话框中点击 Directory,选择Maven的路径,选择到Maven的根目录即可,不需要到bin目录!! 勾选新添加的Maven安装路径,点击Apply 配置Maven的仓库 ...

本地安装Nacos,启动时报错 Unable to start embedded Tomcat

本地安装Nacos,启动时报错 Unable to start embedded Tomcat org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerEx...

多条件查询+反向查询+分段区间数据查询+最后几行的数据+

VLOOKUP和MATCH函数 INDEX和MATCH函数 2.INDEX MATCH 反向查找 反向查找方法2: choose函数 =VLOOKUP(B2,CHOOSE({1,2},$E2:2:2:E9,9,9,D2:2:2:D$9),2,) 反向查找方法3: IF函数 =VLOOKUP(B2,if({1,2},$E2:2:2:E9,9,9,D2:2:2:D$9),2,) 3.分段区间的数据查询...

问答精选

C++ NetBeans - Removing Console from Win32

I have a Win32 app that displays a console window in the back. How can I, using NetBeans/C++, remove this console window? Thanks in advance. you might want to go for "Right-Mouse-Button: Properti...

Node losing gravity after SCNAction

I'd like to drop an object and then move it back to the top and let it fall again. The first part is working, but then the node seems to lose its gravity and isn't falling again. It looks like its phy...

PyQt5 store time of keyPressEvent

Issue: I have a program where I will be showing several pages with a stacked widget, and users will have to press a button (using code I've written below) to go to the next page of the stacked widget....

How to initialize a systemc port name which is an array?

I wanted to initialize a port name. The port is an array and my code does not work. The code below would work by giving clk with a name "clk". However clk port is not an array: How do I name...

Java source code can not be found in eclipse

In an xpages application a javav source code was added to the Local folder within the Lotus nsf file. Now can not be seen, and can not be found with search. The code still woking, but it is not possib...

相关问题

相关文章

热门文章

推荐文章

相关标签

推荐问答