Commons-long compareTo ArrayUtils.toString( )

1.commons-lang.jar中提供了CompareToBuilder类,用于比较两个对象

public int compareTo(Object o){
		int compare=-1;
		if(o!=null&&Person.class.isAssignableFrom(o.getClass())){
			Person p=(Person)o;
			compare=new CompareToBuilder().append(name,p.getName()).append(addr, p.getAddr()).toComparison();
		}
		return compare;
	}

 2.ArrayUtils.toString用于把数组作为字符串输出

public void testToArrays(){
		int[] intArray = new int[] { 2, 3, 4, 5, 6 };
		int[][] multiDimension = new int[][] { { 1, 2, 3 }, { 2, 3 }, {5, 6, 7} };
		String[] strings = new String[] { "Blue", "Green", null, "Yellow" };
		
		List list = new ArrayList( );
		list.add( "Foo" );
		list.add( "Blah" );
		System.out.println( ArrayUtils.toString( list.toArray( ) ) );
		
        System.out.println(ArrayUtils.toString(intArray));
        System.out.println(ArrayUtils.toString(multiDimension));
        System.out.println( "Strings: " + ArrayUtils.toString(strings, "Unknown"));
	}

 {2,3,4,5,6}
{{1,2,3},{2,3},{5,6,7}}
Strings: {Blue,Green,<null>,Yellow}

3.数组克隆和反转

public void testCloneAndReverse(){
		long[] array = { 1, 3, 2, 3, 5, 6 };

		long[] reversed = ArrayUtils.clone(array);

		ArrayUtils.reverse(reversed);

		System.out.println("Original: " + ArrayUtils.toString(array));

		System.out.println("Reversed: " + ArrayUtils.toString(reversed));
	}

 4.Transforming Between Object Arraysand Primitive Arrays

public void testToObject(){
		long[] primitiveArray = new long[] { 12, 100, 2929, 3323 };
		Long[] objectArray = ArrayUtils.toObject(primitiveArray);

		Double[] doubleObjects = new Double[] { new Double(3.22) };
		double[] doublePrimitives = ArrayUtils.toPrimitive(doubleObjects);
	}

 

 5.Finding Items in an Array

 

public void testContains(){
		String[] stringArray = { "Red", "Orange", "Blue", "Brown", "Red" };

		boolean containsBlue = ArrayUtils.contains(stringArray, "Blue");

		int indexOfRed = ArrayUtils.indexOf(stringArray, "Red");

		int lastIndexOfRed = ArrayUtils.lastIndexOf(stringArray, "Red");

		System.out.println("Array contains 'Blue'? " + containsBlue);

		System.out.println("Index of 'Red'? " + indexOfRed);

		System.out.println("Last Index of 'Red'? " + lastIndexOfRed);
	}

   Array contains 'Blue'? true
   Index of 'Red'? 0
   Last Index of 'Red'? 4

你可能感兴趣的:(compareTo)