您的位置:

java倒序,java倒序输出字符串abcde

本文目录一览:

java 集合中怎么将元素倒序排列

方法一:实现Comparable接口排序package collsort.comparable;

package com.cvicse.sort.comparable;

public class Cat implements ComparableCat {

private int age;

private String name;

public Cat(int age, String name) {

this.age = age;

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

......

public int compareTo(Cat o) {

return this.getAge() - o.getAge();

}

......

}

通过实现Comparable接口实现个性化排序测试。排序测试,Collection.sort(list)升序排列Collections.sort(list, Collections.reverseOrder());降序排列;Collections.reverse(list);反转排序,先输出列表最后一个元素

public class TestComparable {

public static void main(String args[]) {

test();

test2();

}

public static void test() {

......

ListCat listCat1 = new ArrayListCat();

Cat cat1 = new Cat(34, "hehe");

Cat cat2 = new Cat(12, "haha");

Cat cat3 = new Cat(23, "leizhimin");

Cat cat4 = new Cat(13, "lavasoft");

listCat1.add(cat1);

listCat1.add(cat2);

listCat1.add(cat3);

......

System.out.println("调用Collections.sort(ListT list)listCat2升序排序:");

Collections.sort(listCat1);

System.out.println("降序排列元素:");

Collections.sort(listCat1, Collections.reverseOrder());

System.out.println("Collections.reverse 从列表中最后一个元素开始输出:");

Collections.reverse(listCat1);

......

}

/**

* 针对数组的排序

*/

public static void test2() {

String[] strArray = new String[] { "z", "a", "C" };

System.out.println("数组转换为列表");

ListString list = Arrays.asList(strArray);

System.out.println("顺序排序列表");

Collections.sort(list);

System.out

.println("按String实现的Comparator对象String.CASE_INSENSITIVE_ORDER排序----");

Collections.sort(list, String.CASE_INSENSITIVE_ORDER);

System.out.println("倒序排序列表");

Collections.sort(list, Collections.reverseOrder());

......

}

}

方法二:实现Comparator接口排序

public class Person {

private int age;

private String name;

......

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

......

}

实现了Comparator接口,重写了compare方法

import java.util.Comparator;

public class PersonComparator implements ComparatorPerson {

public int compare(Person o1, Person o2) {

return o1.getAge() - o2.getAge();

}

}

测试方法

public class TestComparator {

public static void main(String args[]) {

test1();

}

public static void test1() {

System.out.println("升序排序测试:");

ListPerson listPerson = new ArrayListPerson();

Person person1 = new Person(34, "lavasoft");

Person person2 = new Person(12, "lavasoft");

Person person3 = new Person(23, "leizhimin");

Person person4 = new Person(13, "sdg");

listPerson.add(person1);

listPerson.add(person2);

listPerson.add(person3);

ComparatorPerson ascComparator = new PersonComparator();

System.out.println("排序后集合为:");

// 利用Collections类静态工具方法对集合List进行排序

Collections.sort(listPerson, ascComparator);

System.out.println("\n降序排序测试:");

// 从升序排序对象产生一个反转(降序)的排序对象

ComparatorPerson descComparator = Collections

.reverseOrder(ascComparator);

System.out.println("利用反转后的排序接口对象对集合List排序并输出:");

Collections.sort(listPerson, descComparator);

outCollection(listPerson);

}

}

java array倒序

java中有方法直接实现array的倒叙的。可以用Collections的reverse方法即可:

ArrayList al = new ArrayList();

al.add("a");

al.add("b");

//al.add("b");

Collections.reverse(al);

System.out.println(al);

输出结果:[b, a]。

Java怎么倒序string类型?

public static void main(String[] args){\x0d\x0a String aa="hdskjfdi";\x0d\x0a String bb="";\x0d\x0a for(int i=aa.length()-1;i=0;i--){\x0d\x0a bb=bb+aa.charAt(i);\x0d\x0a }\x0d\x0a System.out.println(bb);\x0d\x0a } 其实《此人已挂失》的方法是最简单的,直接调用的方法,我这个是笨一点的方法,可以帮你理解一下过程

Java 数组倒序输出?

1、逆序数组有很多种方法,比如先排序,再逆序存

public static void main(String[] args) {

int[] nums = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};

System.out.print("原数组:");

for (int num : nums) {

System.out.print(num+" ");

}

System.out.print("\n倒序新数组:");

for (int num : reverseArray1(nums)) {

System.out.print(num + " ");

}

}

//1.排序后倒序

public static int[] reverseArray1(int[] nums) {

Arrays.sort(nums);

int[] reNums = new int[nums.length];

for (int i = 0; i nums.length; i++) {

reNums[i] = nums[nums.length - 1 - i];

}

return reNums;

}

//2.Collection 内置的逆序

public static int[] reverseArray2(int[] nums) {

ArrayListInteger list = new ArrayList();

for (int i = 0; i nums.length; i++) {

list.add(nums[i]);

}

Collections.reverse(list);

int [] reNums = new int[nums.length];

for (int i = 0; i nums.length ; i++) {

reNums[i] = list.get(i);

}

return  reNums;

}