关于list倒序的两个小方法
转自:http://www.blogjava.net/jzone/articles/277672.html
第一个:commReverse
这个比较简单,一般都会想到
第二个:stackReverse
这个道理也很简单,利用堆栈原理。对对象进行进出栈操作,形成自然倒序
package
com.yixun.wap;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import org.apache.log4j.Logger;
/**
*
* @descripte reverse list
* @author Gavin.lee
* @date 2009-5-24 13:41:47
* @version 1.0
*/
public class ReverseList {
public static Logger logger = Logger.getLogger(ReverseList.class);
private int id;
private String name;
private String pass;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public List<ReverseList> populate() {//构建list,存放ReverseList实例。
List<ReverseList> list = new ArrayList<ReverseList>();
for (int i = 0; i < 4; i++) {
ReverseList test = new ReverseList();
test.setId(i);
test.setName("lee");
test.setPass("pass");
list.add(test);
}
return list;
}
public void commRervese() {
List<ReverseList> list = this.populate();
for (int m = list.size() - 1; m >= 0; m--) {
ReverseList reverseList = list.get(m);
logger.debug(reverseList.getId());
logger.debug(reverseList.getName());
logger.debug(reverseList.getPass());
}
}
public void stackReverse() {
List<ReverseList> list = this.populate();
Stack<ReverseList> stack = new Stack<ReverseList>(); //构建堆栈,并对list进行进出栈操作。
List<ReverseList> tempList = new ArrayList<ReverseList>();
for (ReverseList t : list)
stack.push(t);
while (!stack.empty())
tempList.add(stack.pop());
for (ReverseList string : tempList) { //获取到倒序后的list。
logger.debug(string.getId());
logger.debug(string.getName());
logger.debug(string.getPass());
}
}
public static void main(String[] args) {
new ReverseList().stackReverse();
new ReverseList().commRervese();
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import org.apache.log4j.Logger;
/**
*
* @descripte reverse list
* @author Gavin.lee
* @date 2009-5-24 13:41:47
* @version 1.0
*/
public class ReverseList {
public static Logger logger = Logger.getLogger(ReverseList.class);
private int id;
private String name;
private String pass;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public List<ReverseList> populate() {//构建list,存放ReverseList实例。
List<ReverseList> list = new ArrayList<ReverseList>();
for (int i = 0; i < 4; i++) {
ReverseList test = new ReverseList();
test.setId(i);
test.setName("lee");
test.setPass("pass");
list.add(test);
}
return list;
}
public void commRervese() {
List<ReverseList> list = this.populate();
for (int m = list.size() - 1; m >= 0; m--) {
ReverseList reverseList = list.get(m);
logger.debug(reverseList.getId());
logger.debug(reverseList.getName());
logger.debug(reverseList.getPass());
}
}
public void stackReverse() {
List<ReverseList> list = this.populate();
Stack<ReverseList> stack = new Stack<ReverseList>(); //构建堆栈,并对list进行进出栈操作。
List<ReverseList> tempList = new ArrayList<ReverseList>();
for (ReverseList t : list)
stack.push(t);
while (!stack.empty())
tempList.add(stack.pop());
for (ReverseList string : tempList) { //获取到倒序后的list。
logger.debug(string.getId());
logger.debug(string.getName());
logger.debug(string.getPass());
}
}
public static void main(String[] args) {
new ReverseList().stackReverse();
new ReverseList().commRervese();
}
}
2009年7月14日23:15:42 PS:
还可以利用lsit中的reverse方法
public
void
reverse()
{
List<ReverseList> list = this.populate();
for(ReverseList t: list) {
System.out.println("id:" + t.getId());
}
Collections.reverse(list);
for(ReverseList t: list) {
System.out.println("id:" + t.getId());
}
}
List<ReverseList> list = this.populate();
for(ReverseList t: list) {
System.out.println("id:" + t.getId());
}
Collections.reverse(list);
for(ReverseList t: list) {
System.out.println("id:" + t.getId());
}
}