zoukankan      html  css  js  c++  java
  • java开始到熟悉62

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

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

    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.打印数组

     1 package array;
     2 
     3 import java.util.Arrays;
     4 
     5 public class array {
     6     public static void main(String[] args)
     7     {
     8         int[] a={1,2,43,12,5,65,23};
     9         System.out.println(a);
    10         System.out.println(Arrays.toString(a));
    11         Arrays.sort(a);
    12         System.out.println(Arrays.toString(a));
    13     }
    14 }

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

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

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

    2,数组排序

    冒泡排序

     1 package array;
     2 
     3 import java.util.Arrays;
     4 
     5 public class maopao {
     6     public static void main(String[] args)
     7     {
     8         int[] a={1,13,4,2,5,23,6,9,3};
     9         sort(a);
    10         System.out.println(Arrays.toString(a));
    11     }
    12     public static void sort(int[] a)
    13     {
    14         int i,j;
    15         int temp=0;
    16         for(i=0;i<a.length;i++)
    17         {
    18             for(j=0;j<a.length-1-i;j++)
    19             {
    20                 if(a[j]>a[j+1])
    21                 {
    22                     temp=a[j];
    23                     a[j]=a[j+1];
    24                     a[j+1]=temp;
    25                 }
    26             }
    27         }
    28     }
    29 
    30 }

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

    3、二分法查找

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

     1 package array;
     2 
     3 import java.util.Arrays;
     4 
     5 /**
     6  * 二分法查找
     7  * @author acer
     8  *
     9  */
    10 public class binarysearch {
    11     public static void main(String[] args)
    12     {
    13         int searchmath=65;//查找的数
    14         int[] a={56,100,23,42,37,120,12,80,94,65,76};
    15         System.out.printf("普通查找%d的循环次数%d
    ",searchmath,generalLoop(a,searchmath));
    16         System.out.printf("二分查找%d的循环次数%d",searchmath,binarySearch(a,searchmath));
    17     }
    18     public static int generalLoop(int[] a,int searchmath)
    19     {
    20         int i;
    21         int searchcount=0;
    22         for(i=0;i<a.length;i++)
    23         {
    24             searchcount++;
    25             if(a[i]==searchmath)
    26                 break;
    27         }
    28         return searchcount;
    29     }
    30     public static int binarySearch(int[] a,int searchmath)
    31     {
    32         Arrays.sort(a);//首先对数组进行排序
    33         System.out.println("数组已排序");
    34         int i=0;
    35         int index=0;
    36         int start=0;//开始位置
    37         int end=a.length-1;//结束位置
    38         int searchcount=0;
    39         for(i=0;i<a.length;i++)
    40         {
    41             searchcount++;
    42             index=(start+end)/2;
    43             if(a[index]<searchmath)
    44             {
    45                 start=index;
    46             }
    47             else if(a[index]>searchmath)
    48             {
    49                 end=index;
    50             }
    51             else
    52             {
    53                 break;
    54             }
    55         }
    56         return searchcount;
    57     }
    58 }

    运行结果:

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

  • 相关阅读:
    利用JasperReport+iReport进行Web报表开发
    EEPlat PaaS VS Saleforce force.com
    Python用subprocess的Popen来调用系统命令
    最短路径A*算法原理及java代码实现(看不懂是我的失败)
    Java抓取网页数据(原网页+Javascript返回数据)
    Atitit.dwr3 不能显示错误具体信息的解决方式,控件显示错误具体信息的解决方式 java .net php
    白话经典算法系列之五 归并排序的实现
    poj 百练 2765 八进制小数(精度问题)
    winzip15.0注冊码
    Python:渗透测试开源项目
  • 原文地址:https://www.cnblogs.com/xiaojingang/p/3702028.html
Copyright © 2011-2022 走看看