java.util.Arrays使用例子

来源:互联网 发布:软件开发好不好学 编辑:程序博客网 时间:2024/05/21 03:26
package zhiru.com;import java.util.Arrays;public class TestArrays {private int[] myArray;TestArrays(int[]a){if(a!=null){myArray=new int[a.length];for(int i=0;i<a.length;i++){myArray[i]=a[i];}}}//调用Arrays的sort方法对数组进行排序。public void sortArray(){Arrays.sort(myArray);System.out.println("排序后的数组:"+Arrays.toString(myArray));}//调用binarySearch前必须排好序。public int binarySearchArray(int val){return Arrays.binarySearch(myArray, val);}//元素全部重置为零.public void setZeros(){Arrays.fill(myArray, 0);System.out.println("数组置零:"+Arrays.toString(myArray));}public boolean equalsArray(int[]a){return Arrays.equals(myArray, a);}public int caluHashCode(){return Arrays.hashCode(myArray);}}

package zhiru.com;import java.io.UnsupportedEncodingException;/* * this class is just for the test of String Class. */public class TestString {/** * @param s */public static void printStr(String s){System.out.println(s);}/** * @param args * @throws UnsupportedEncodingException  */public static void main(String[] args) throws UnsupportedEncodingException {// TODO Auto-generated method stub//init a String object//use two ways to init String object.String myStr="hi string";String str="hi string";/* * == 比较的是基本数据类型和引用 * equals是边角对象的内容是否一致。 * 字符串对象虽然内容一致,但是是不同的对象,所以通过==比较结果为false. */System.out.println(myStr==str);//trueString str1=new String("hi string");String str2=new String("hi string");System.out.println(str==str1);//falseSystem.out.println(str.equals(str1));//trueSystem.out.println(str2.equals(str));//trueSystem.out.println(str1==str2);//falseString myStr1=new String("hi string");printStr(myStr);printStr(myStr);/* * charAt 方法是根据指定的索引值,获取字符串中的字符。 */char c=myStr.charAt(5);System.out.println(c);/* * compareTo  * 该方法的作用是比较两个字符串的大小, * 比较的原理是依次比较每个字符的字符编码。 * 首先比较两个字符串的第一个字符, * 如果第一个字符串的字符编码大于第二个的字符串的字符编码, * 则返回大于0的值,如果小于则返回小于0的值,如果相等则比较后续的字符, * 如果两个字符串中的字符编码完全相同则返回0 */System.out.println("abc".compareTo("abd"));/* * + 加号可以用来连接两个字符串,但是效率比较低。 * 因为String类是final类型的,会在堆中重新创建对象。 * 占用内存,降低效率。 */printStr("123"+"abc");/* * endsWith 该方法的作用是判断字符串是否以某个字符串结尾, * 如果以对应的字符串结尾,则返回true。 */System.out.println("love.txt".endsWith(".doc"));/* * equals  * 作用是判断两个字符串对象的内容是否相同 */System.out.println("abc".equals("123"));/* * getBytes * 该方法的作用是将字符串转换为对应的byte数组,从而便于数据的存储和传输。 */String s="计算机";byte[]b=s.getBytes("utf8");System.out.println(b[1]);/* * indexOf *  int indexOf(int ch)Returns the index within this string of the first occurrence of the specified character.intindexOf(int ch, int fromIndex)Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.intindexOf(String str)Returns the index within this string of the first occurrence of the specified substring.intindexOf(String str, int fromIndex)Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. */int index="abcdeabc".indexOf('c');int sindex="abcdeabc".indexOf("abc");int index1="abcdeabc".indexOf('a',4);int index2="abcdeabc".indexOf("eab",3);System.out.println(""+index+sindex+index1+index2);/* * length() * 获取字符串长度的方法 * 一个汉字也是一个字符. */System.out.println("abcde你好".length());/* * replace(char oldChar, char newChar) * replace(CharSequence target, CharSequence replacement) */System.out.println("ssss".replace('s', 'x'));/* * split */System.out.println("a,b,c,d".split(",")[1]);/* * substring * substring(int start) * substring(int start,int end) */printStr(myStr.substring(2));printStr(myStr1.substring(2,myStr1.length()-1));/* * toCharArray * 把字符串转换为字符串数组。 */char[] a=myStr.toCharArray();System.out.println(a);/* * trim * 将字符串开始和结尾的空格删除。 */printStr(" 234 567 890 111213 ".trim());/* * valueOf * 将基本数据类型转换为字符串类型 */char[]x={'a','b','c'};printStr(String.valueOf(123));//intprintStr(String.valueOf(x));//char []printStr(String.valueOf(true));//boolprintStr(String.valueOf('s'));//charprintStr(String.valueOf(34.2f));//floatprintStr(String.valueOf(12.014919620567));//doubleprintStr(String.valueOf(12357L));//long//test the class TestArraysint[]a1={1,3,4,2,6,8,7,8,10};TestArrays ta=new TestArrays(a1);int []x1={2,3,4,5,6,7,8,9,0};System.out.println(ta.equalsArray(x1));System.out.println("哈希值为:"+ta.caluHashCode());ta.sortArray();int val=6;System.out.println(""+val+"在:"+ta.binarySearchArray(val));ta.setZeros();}}

0 0
原创粉丝点击