LinkedList

託帕發表於2018-09-05
import java.util.*;
public class Test1 {
	public static void main(String[] args){
		LinkedList bb=new LinkedList();
		Sp sp1=new Sp("001","話梅",5f);
		Sp sp2=new Sp("002","薯片",8f);
		/*
		bb.addFirst(sp1);
		bb.addFirst(sp2);

		for(int i=0;i<bb.size();i++){
			System.out.println(((Sp)bb.get(i)).getMingcheng());//薯片  話梅
			System.out.println(((Sp)bb.getFirst()).getMingcheng());//薯片  薯片
			System.out.println(((Sp)bb.getLast()).getMingcheng());//話梅  話梅
		}
		 */

		bb.addLast(sp1);
		bb.addLast(sp2);

		for(int i=0;i<bb.size();i++){
			//System.out.println(((Sp)bb.get(i)).getMingcheng());//話梅  薯片
			System.out.println(((Sp)bb.getFirst()).getMingcheng());//話梅  話梅
			//System.out.println(((Sp)bb.getLast()).getMingcheng());//薯片 薯片
		}

	}
}

class Sp{
	private String bianhao;
	private String mingcheng;
	private float jiage;

	Sp(String bianhao,String mingcheng,float jiage){
		this.bianhao=bianhao;
		this.mingcheng=mingcheng;
		this.jiage=jiage;
	}

	public String getBianhao() {
		return bianhao;
	}

	public void setBianhao(String bianhao) {
		this.bianhao = bianhao;
	}

	public String getMingcheng() {
		return mingcheng;
	}

	public void setMingcheng(String mingcheng) {
		this.mingcheng = mingcheng;
	}

	public float getJiage() {
		return jiage;
	}

	public void setJiage(float jiage) {
		this.jiage = jiage;
	}	
}

 

相關文章