(说明:昨天网络出现了问题导致昨天的没有按时上传,这篇算是昨天的,今天晚上照常上传今天的内容)

本次主题:数组拷贝、排序、二分法

1、数组拷贝

a.java.lang中System 类包含一些有用的类字段和方法。它不能被实例化。

System 类提供的设施中,有标准输入、标准输出和错误输出流;对外部定义的属性和环境变量的访问;加载文件和库的方法;还有快速复制数组的一部分的实用方法。

public static void arraycopy(Object src, int srcPos, Object dest,  int destPos,
                 int length)
从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。从 src 引用的源数组到 dest 引用的目标数组,数组组件的一个子序列被复制下来。被复制的组件的编号等于 length 参数。源数组中位置在 srcPossrcPos+length-1 之间的组件被分别复制到目标数组中的 destPosdestPos+length-1 位置。

b.java.util中的Arrays类,此类包含用来操作数组(比如排序和搜索)的各种方法。此类还包含一个允许将数组作为列表来查看的静态工厂。

toString

public static String toString(short[] a)
返回指定数组内容的字符串表示形式。字符串表示形式由数组的元素列表组成,括在方括号("[]")中。相邻元素用字符 ", "(逗号加空格)分隔。这些元素通过 String.valueOf(short) 转换为字符串。如果 anull,则返回 "null"

c.打印数组

 package array;

 import java.util.Arrays;

 public class array {
public static void main(String[] args)
{
int[] a={,,,,,,};
System.out.println(a);
System.out.println(Arrays.toString(a));
Arrays.sort(a);
System.out.println(Arrays.toString(a));
}
}

运行结果:
[I@19836ed
[1, 2, 43, 12, 5, 65, 23]
[1, 2, 5, 12, 23, 43, 65]

直接打印a结果可能是地址,[I代表是一个int数组,‘@’后面跟的是数组所在的地址。

d.对象也可以排序,不过要自己定义compareTo方法,这个放到后面容器的地方记录。

2,数组排序

冒泡排序

 package array;

 import java.util.Arrays;

 public class maopao {
public static void main(String[] args)
{
int[] a={,,,,,,,,};
sort(a);
System.out.println(Arrays.toString(a));
}
public static void sort(int[] a)
{
int i,j;
int temp=;
for(i=;i<a.length;i++)
{
for(j=;j<a.length--i;j++)
{
if(a[j]>a[j+])
{
temp=a[j];
a[j]=a[j+];
a[j+]=temp;
}
}
}
} }

运行结果: [1, 2, 3, 4, 5, 6, 9, 13, 23]

3、二分法查找

二分法查找前提必须是有序的(升序或降序)

 package array;

 import java.util.Arrays;

 /**
* 二分法查找
* @author acer
*
*/
public class binarysearch {
public static void main(String[] args)
{
int searchmath=;//查找的数
int[] a={,,,,,,,,,,};
System.out.printf("普通查找%d的循环次数%d\n",searchmath,generalLoop(a,searchmath));
System.out.printf("二分查找%d的循环次数%d",searchmath,binarySearch(a,searchmath));
}
public static int generalLoop(int[] a,int searchmath)
{
int i;
int searchcount=;
for(i=;i<a.length;i++)
{
searchcount++;
if(a[i]==searchmath)
break;
}
return searchcount;
}
public static int binarySearch(int[] a,int searchmath)
{
Arrays.sort(a);//首先对数组进行排序
System.out.println("数组已排序");
int i=;
int index=;
int start=;//开始位置
int end=a.length-;//结束位置
int searchcount=;
for(i=;i<a.length;i++)
{
searchcount++;
index=(start+end)/;
if(a[index]<searchmath)
{
start=index;
}
else if(a[index]>searchmath)
{
end=index;
}
else
{
break;
}
}
return searchcount;
}
}

运行结果:

普通查找65的循环次数10
数组已排序
二分查找65的循环次数1

最新文章

  1. JS字符串替换函数:Replace(“字符串1″, “字符串2″),
  2. android学习疑问汇兑
  3. Linux rpm 命令参数使用详解[介绍和应用]
  4. 【转载】C++编译出现 error C2664: 不能将参数 2 从“const char [5]”转换为“LPCTSTR”解决办法。
  5. 演练:Office 编程(C# 和 Visual Basic)
  6. word2010 ctrl v not work
  7. HDU 1159
  8. "判断this指针是不是null有什么意义呢"
  9. win10 64bit 安装scrapy-1.1
  10. C++_内部类
  11. Lucene.NET中Field.Index 和 Field.Store的几种属性的用法
  12. 建立、配置和使用Activity——使用Bundle在Activity之间交换数据
  13. OCR智能识别身份信息
  14. 递归一个List&lt;T&gt;,可自己根据需要改造为通用型。
  15. python全栈开发day99-DRF序列化组件
  16. HDU 5178 pairs【二分】||【尺取】
  17. Android学习之基础知识四-Activity活动7讲(活动的启动模式)
  18. 根据userAgent判断打开网页的所在终端,以及浏览器
  19. 什么是K线?K线的详解!
  20. 前端模块化(AMD和CMD、CommonJs)

热门文章

  1. Ubuntu 16.04 LTS上git提交出现警告Warning: Permanently added &#39;github.com,52.74.223.119&#39; (RSA) to the list of known hosts. 的解决方法
  2. 网址导航[IT]
  3. 九度oj 题目1100:最短路径
  4. P1736 创意吃鱼法 (动态规划)
  5. 【双向bfs】2017多校训练十 HDU 6171 Admiral
  6. 【2018.10.20】noip模拟赛Day3 二阶和
  7. php 学习随笔
  8. bzoj2144 跳跳棋 二分
  9. ElasticSearch 多索引
  10. 多线程之 Volatile 变量 详解