本文目录一览:
- 1、java linked list里的元素顺序反过来
- 2、对List集合倒序排序怎么排
- 3、java怎么把list元素倒转
- 4、求java一个程序补全(list逆序输出与查找)
- 5、java 中,如何将1~100的偶数所组成的List集合倒序排列,谢谢!
- 6、java 集合中怎么将元素倒序排列
java linked list里的元素顺序反过来
定义一个LinkedListInteger templist = new LinkedList();来存储list里面的值,通过迭代list,将值插入在templist的头上,那么templist就是list的反转了,最后将templist赋值给list就行了!
如下代码:
public void reverse() {
LinkedListInteger list = new LinkedList();
LinkedListInteger templist = new LinkedList();
int i = 0;
while (i 6) {
list.add(i);
i++;
}
IteratorInteger it = list.iterator();
int m;
while (it.hasNext() i = 0) {
m = it.next();
templist.addFirst(m);
i--;
}
list = templist;
System.out.println(list);
}
运行结果为:
5 4 3 2 1 0
从API中可以看到List等Collection的实现并没有同步化,如果在多线程应用程序中出现同时访问,而且出现修改操作的时候都要求外部操作同步化;调用Iterator操作获得的Iterator对象在多线程修改Set的时候也自动失效,并抛出java.util.ConcurrentModificationException。这种实现机制是fail-fast,对外部的修改并不能提供任何保证。
Iterator是工作在一个独立的线程中,并且拥有一个 mutex锁,就是说Iterator在工作的时候,是不允许被迭代的对象被改变的。
Iterator被创建的时候,建立了一个内存索引表(单链表),这个索引表指向原来的对象,当原来的对象数量改变的时候,这个索引表的内容没有同步改变,所以当索引指针往下移动的时候,便找不到要迭代的对象,于是产生错误。
List、Set等是动态的,可变对象数量的数据结构,但是Iterator则是单向不可变,只能顺序读取,不能逆序操作的数据结构,当 Iterator指向的原始数据发生变化时,Iterator自己就迷失了方向。
所以如果像下面这么写就会抛出异常java.util.ConcurrentModificationException
:
public void reverse() {
LinkedListInteger list = new LinkedList();
int i = 0;
while (i 6) {
list.add(i);
i++;
}
IteratorInteger it = list.iterator();
int m;
while (it.hasNext() i = 0) {
m = it.next();
list.add(m);
list.remove(0);
i--;
}
System.out.println(list);
}
对List集合倒序排序怎么排
[size=large][color=red]要对List排序,你要对List里装的这种类型的类实现排序接口(Comparable).
举个例子:
下面这个例子是对ListMyObject进行排序.使用Collections.sort(List list);方法,此方法会调用MyObject的compareTo方法.所以在MyObject类定义里要实现compareTo方法.
Java代码
public class ListSort {
/**
* main()
* 2010-4-2,下午09:25:57
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ListMyObject lists = new ArrayListMyObject();
MyObject obj1 = new MyObject("d");
MyObject obj2 = new MyObject("a");
lists.add(obj1);
lists.add(obj2);
Collections.sort(lists);
for (MyObject myObject : lists) {
System.out.println(myObject.getContent());
}
}
}
class MyObject implements ComparableMyObject{
private String content;
public MyObject(String _content) {
this.content = _content;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int compareTo(MyObject obj) {
// TODO Auto-generated method stub
if (null == obj) return 1;
else {
return this.content.compareTo(obj.content);
}
}
}
下面是Collections.sort方法
Java代码
public static T extends Comparable? super T void sort(ListT list) {
Object[] a = list.toArray();
Arrays.sort(a);
ListIteratorT i = list.listIterator();
for (int j=0; ja.length; j++) {
i.next();
i.set((T)a[j]);
}
}
实际上我们的MyObject类的方法compareTo是在Arrays.sort()中被调用的...
请看..
Java代码
public static void sort(Object[] a) {
Object[] aux = (Object[])a.clone();
mergeSort(aux, a, 0, a.length, 0);
}
Java代码
private static void mergeSort(Object[] src,
Object[] dest,
int low,
int high,
int off) {
int length = high - low;
// Insertion sort on smallest arrays
if (length INSERTIONSORT_THRESHOLD) {
for (int i=low; ihigh; i++)
for (int j=i; jlow
((Comparable) dest[j-1]).compareTo(dest[j])0; j--)
swap(dest, j, j-1);
return;
}
// Recursively sort halves of dest into src
int destLow = low;
int destHigh = high;
low += off;
high += off;
int mid = (low + high) 1;
mergeSort(dest, src, low, mid, -off);
mergeSort(dest, src, mid, high, -off);
// If list is already sorted, just copy from src to dest. This is an
// optimization that results in faster sorts for nearly ordered lists.
if (((Comparable)src[mid-1]).compareTo(src[mid]) = 0) {
System.arraycopy(src, low, dest, destLow, length);
return;
}
// Merge sorted halves (now in src) into dest
for(int i = destLow, p = low, q = mid; i destHigh; i++) {
if (q = high || p mid ((Comparable)src[p]).compareTo(src[q])=0)
dest[i] = src[p++];
else
dest[i] = src[q++];
}
}
[/color][/size]
当然,这是一种实现排序的办法.还有一种是实现Comparator,实现这个接口,然后使用
Collections.sort(List list,Comparator c);这个方法来排序..
java怎么把list元素倒转
这个比较简单:
ArrayListString list=new ArrayListString();
for(int i=0;i=5;i++){
list.add("num"+i);
}
StringBuilder b = new StringBuilder(list.toString());
System.out.println(b.reverse());
好多方法,不会问我
求java一个程序补全(list逆序输出与查找)
不会又是你吧。刚好我今天没什么事
// Q2
static void DisplayReverse(ListString element) {
System.out.print("null");
ListString [] ls=new ListString[numOfElements];
int i=0;
while (element != null) {
ls[i]=element;
element = element.next;
i++;
}
for (int j = ls.length-1; j =0; j--) {
System.out.print("--"+ls[j].name + "[" + ls[j].id + "]" );
}
}
// Q3
static void Search(String query, ListString head) {
int i=0;
while (head != null) {
if(head.name.equals(query)){
i++;
}
head = head.next;
}
if(i0){
System.out.println("[Search] found:"+query+"["+i+"]");
}else{
System.out.println("[Search] not found: "+query);
}
}
java 中,如何将1~100的偶数所组成的List集合倒序排列,谢谢!
public class SortNums {
public static void main(String[] s){
ListInteger list = new ArrayListInteger();
for(int i=1;i=100;i++){
if(i%2==0){
list.add(i);
}
}
System.out.println(list);
Collections.sort(list,Collections.reverseOrder());
System.out.println(list);
}
}
好简单的,输出结果为:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]
[100, 98, 96, 94, 92, 90, 88, 86, 84, 82, 80, 78, 76, 74, 72, 70, 68, 66, 64, 62, 60, 58, 56, 54, 52, 50, 48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2]
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);
}
}