【Java数组】

1. 用sort()方法对Java数组进行排序,及如何使用 binarySearch() 方法来查找数组中的元素

binarySearch()

返回值:

如果它包含在数组中,则返回搜索键的索引;否则返回 (-(插入点) - 1)

插入点被定义为将键插入数组的那一点:即第一 个大于此键的元素索引,如果数组中的所有元素都小于指定的键,则为 a.length。注意,这保证了当且仅当此键被找到时,返回的值将 >= 0。

否则返回 (-(插入点) - 1)这句话要注意:要是查询的的值小于数组里面的最小值那么结果(-(0)-1结果就是-1),如果查询的 值大于数组里面的最大值。那么结果就是(-(它的索引值)-1结果就是-(1+索引值))

2. 使用sort()方法对Java数组进行排序,及如何使用 insertElement () 方法向数组插入元素

    //  insertElement () 向数组插入元素,自定义方法

  System.arraycopy()

System提供了一个静态方法arraycopy(),我们可以使用它来实现数组之间的复制。

其函数原型是: public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

src:源数组; srcPos:源数组要复制的起始位置; dest:目的数组; destPos:目的数组放置的起始位置; length:复制的长度。

注意:src and dest都必须是同类型或者可以进行转换类型的数组。

有
趣的是这个函数可以实现自己到自己复制,比如: int[] fun ={0,1,2,3,4,5,6};
System.arraycopy(fun,0,fun,3,3); 则结果为:{0,1,2,0,1,2,6};
实现过程是这样的,先生成一个长度为length的临时数组,将fun数组中srcPos
到srcPos+length-1之间的数据拷贝到临时数组中,再执行System.arraycopy(临时数组,0,fun,3,3).

3. 使用数组的属性 length 来获取数组的长度

4. Collections.reverse(ArrayList) 将数组进行反转

5. 通过循环输出数组:

/*
author by w3cschool.cc
文件名:Main.java
*/ public class Welcome {
public static void main(String[] args){
String[] greeting = new String[3];
greeting[0] = "This is the greeting";
greeting[1] = "for all the readers from";
greeting[2] = "Java Source .";
for (int i = 0; i < greeting.length; i++){
System.out.println(greeting[i]);
}
}
}

6. 通过 Collection 类的 Collection.max() 和 Collection.min() 方法来查找数组中的最大和最小值

/*
author by w3cschool.cc
文件名:Main.java
*/ import java.util.Arrays;
import java.util.Collections; public class Main {
public static void main(String[] args) {
Integer[] numbers = { 8, 2, 7, 1, 4, 9, 5};
int min = (int) Collections.min(Arrays.asList(numbers));
int max = (int) Collections.max(Arrays.asList(numbers));
System.out.println("最小值: " + min);
System.out.println("最大值: " + max);
}
}

说明:

asList方法:当使用asList()方法时,数组就和列表链接在一起了。当更新其中之一时,另一个将自动获得更新。注意:仅仅针对对象数组类型,基本数据类型数组不具备该特性 。

Link:http://blog.csdn.net/cntanghai/article/details/7188296

7. 通过 List 类的 Arrays.toString () 方法和 List 类的 list.Addall(array1.asList(array2) 方法将两个数组合并为一个数组:

/*
author by w3cschool.cc
文件名:Main.java
*/ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; public class Main {
public static void main(String args[]) {
String a[] = { "A", "E", "I" };
String b[] = { "O", "U" };
List list = new ArrayList(Arrays.asList(a));
list.addAll(Arrays.asList(b));
Object[] c = list.toArray();
System.out.println(Arrays.toString(c));
}
}

8. Array.fill(arrayname,value) 方法和 Array.fill(arrayname ,starting index ,ending index ,value) 方法向数组中填充元素

9. 数组扩容

public class Main {
public static void main(String[] args) {
String[] names = new String[] { "A", "B", "C" };
String[] extended = new String[5];
extended[3] = "D";
extended[4] = "E";
System.arraycopy(names, 0, extended, 0, names.length);
for (String str : extended){
System.out.println(str);
}
}
}

10. 用 sort () 和 binarySearch () 方法来对数组进行排序及查找数组中的元素

11. remove () 方法来删除数组元素

12. removeAll () 方法来计算两个数组的差集

13. removeAll () 方法来删除包含在另外一个数组中的数组元素——求交集

http://www.runoob.com/java/arrays-retainall.html

14. contains () 方法来查找数组中的指定元素:

/*
author by w3cschool.cc
文件名:Main.java
*/ import java.util.ArrayList; public class Main {
public static void main(String[] args) {
ArrayList objArray = new ArrayList();
ArrayList objArray2 = new ArrayList();
objArray2.add(0,"common1");
objArray2.add(1,"common2");
objArray2.add(2,"notcommon");
objArray2.add(3,"notcommon1");
objArray.add(0,"common1");
objArray.add(1,"common2");
System.out.println("objArray 的数组元素:"+objArray);
System.out.println("objArray2 的数组元素:"+objArray2);
System.out.println("objArray 是否包含字符串common2? : "
+objArray.contains("common1"));
System.out.println("objArray2 是否包含数组 objArray? :"
+objArray2.contains(objArray) );
}
}

结果:

objArray 的数组元素:[common1, common2]
objArray2 的数组元素:[common1, common2, notcommon, notcommon1]
objArray 是否包含字符串common2? : true
objArray2 是否包含数组 objArray? :false

15. equals ()方法来判断数组是否相等

Arrays.equals(ary, ary1));

16.union ()方法求字符串并集

String[] result_union = union(arr1, arr2);

  for (a : b) { } // 遍历数组

.Collection是接口,是容器类的总接口。像那些子接口:set,Queue,List等都继承了这个接口,像实现类:HashSet、ArrayList等都实现了对应的Set、List等接口。
要获取里面的元素很简单,在方法体中,像你代码中的“RosterEntry entry”对象“entry”就是。 .for(:){……}这个是个JDK1.5的增强for循环也叫for-each.是为了方便遍历数组和collection的.由于collection都实现了Iterable(可迭代的)接口,所以可以用for-each来遍历容器类,示例一:
List<String> list=new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
for (String str : list) {
System.out.println(str);
} 另外我最常用的就是遍历数组,示例二
int[] arr = new int[];
arr[] = ;
arr[] =;
arr[] = ;
for(int i : arr){
System.out.println(i);
}

Java方法-数组的更多相关文章

  1. wrong number of arguments,java方法反射时数组参数的坑

    java方法中只有一个参数是数组,反射的时候我们不能想当然的传歌数组进去,传数组进去的时候表示多个参数. 两个数组不是一个意思啊. 我们应该把数组转为objet,这样才表示一个参数. import j ...

  2. java去除数组中重复的元素方法总结

    /* * ArrayUnique.java * Version 1.0.0 * Created on 2017年12月16日 * Copyright ReYo.Cn */ package reyo.s ...

  3. 将java中数组转换为ArrayList的方法实例(包括ArrayList转数组)

    方法一:使用Arrays.asList()方法   1 2 String[] asset = {"equity", "stocks", "gold&q ...

  4. Java基础(五):数组和Java方法

    一.Java数组: 1.声明数组变量: 首先必须声明数组变量,才能在程序中使用数组.下面是声明数组变量的语法:注意: 建议使用 dataType[] arrayRefVar 的声明风格声明数组变量. ...

  5. 【Java面试题】18 java中数组有没有length()方法?string没有lenght()方法?下面这条语句一共创建了多少个对象:String s="a"+"b"+"c"+"d";

    数组没有length()这个方法,有length的属性.String有有length()这个方法. int a[]; a.length;//返回a的长度 String s; s.length();// ...

  6. java中数组有没有length()方法?string没有lenght()方法?

    java中数组有没有length()方法,求数组的长度可以使用数组的length属性. int[] arr={1,2,3,4,5}; int length=arr.length;//求数组的长度 -- ...

  7. Java 方法与数组

    方法 什么是方法? 方法定义:方法(Method),函数(function),其实就指一个特定的功能操作,程序中完成独立功能,可重复使用的一段代码的集合. 方法的定义 方法定义格式: [修饰符] 返回 ...

  8. Java方法调用数组,是否改变原数组元素的总结

    Java方法调用数组,是否改变原数组元素的总结 //个人理解, 欢迎吐槽 注意String是引用型变量, 我的理解也就是指向型, 指向一个数据或变量, 画图理解最容易, string 指向的 数据的值 ...

  9. 067 01 Android 零基础入门 01 Java基础语法 08 Java方法 05 数组作为方法参数

    067 01 Android 零基础入门 01 Java基础语法 08 Java方法 05 数组作为方法参数 本文知识点:数组作为方法参数 说明:因为时间紧张,本人写博客过程中只是对知识点的关键步骤进 ...

随机推荐

  1. Tomcat架构(二)

    5.容器组件 这一小节中我们将讨论请求处理组件:引擎(engine).虚拟主机.上下文(context)组件. 5.1.引擎(engine) 引擎表示可运行的Catalina的servlet引擎实例并 ...

  2. 将数组转换成List

    采用比较好的方法Collections.addAll(arrayList, arr); arrayList是一个List arr是一个数组 并且要求arrayList与arr的类型是相同的  或    ...

  3. 【转】Linux下svn的常用工作流程

    原文网址:http://www.cnblogs.com/cobbliu/archive/2011/07/08/2389011.html 上篇文章在ubuntu和redhat5.5上搭建好了svnser ...

  4. Delphi webservice 定义 转

    webservice   Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务. 简介 它是一种构建应用程序的普遍 ...

  5. UVA 11796 Dog Distance(向量)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=31962 [代码] #include<cstdio> # ...

  6. JavaScript高级程序设计1.pdf

    第一遍通读的时候对JavaScript一点都不了解翻了一整本书仅仅是眼熟的几个名词,现在会写一些js效果了,对程序有一定的认知,又要在读一遍,再加深一些了解,当然以后还会有第三遍第四遍,等完全啃透了这 ...

  7. [转载]Div和Table的区别

    1:速度和加载方式方面的区别 div 和 table 的差异不是速度,而是加载方式,速度只能是指网络速度,如果速度足够快,是没有差异的: div 的加载方式是即读即加载,遇到 <div> ...

  8. 【C++】冒泡排序、插入排序、快速排序

    #include<iostream> using namespace std; void BubbleSort(int *a,int istart,int len)//冒泡排序 { //a ...

  9. How to make apq8084

    1.first of all ,you will meet many problem no excute permisson,pls do : chmod -R +x APQ8084/ 2. buil ...

  10. expect远程登陆hw交换机--测试通过1

    #!/bin/bash #connect_the_hw_switch #by osx1260@.com swip=172.16.56.2 swport= swpasswd='yourswpasswd' ...