Java 从主类中外包堆栈和文件读取器
你好,我有点迷路了,需要一些帮助。 基本上我得到了一堆对象,在这个例子中是字符串Java 从主类中外包堆栈和文件读取器,java,android,stack,Java,Android,Stack,你好,我有点迷路了,需要一些帮助。 基本上我得到了一堆对象,在这个例子中是字符串 public class Showall extends ActionBarActivity implements OnClickListener { Stapel<String> s = new EVLStapel<String>(); @Override public void onCreate(Bundle savedInstance
public class Showall extends ActionBarActivity implements OnClickListener {
Stapel<String> s = new EVLStapel<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
add();
setContentView(R.layout.activity_showall);
init();
}
这里是init()的重要部分:
所以现在我的问题来了。我想把我的堆栈和add方法外包出去,因为我将在许多不同的类中使用这个方法,如果我能为我的堆栈和我的filereader创建一个单独的类,工作就会少很多。
但我不知道怎么做。
我试过这样的方法:
public class Reader extends ActionBarActivity {
Stapel<String> s = new EVLStapel<String>();
public void add(){
AssetManager mctr;
String zeile = null;
try{
mctr = getAssets();
InputStream is = mctr.open("ausgabe.txt");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while((zeile = br.readLine()) != null)
{
String[] parts = zeile.split(",");
s.push(parts[0], parts[1], parts[2]);
zeile = br.readLine();
}
br.close();
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
编辑:
EVLStapel代码:
public class EVLStapel<T> implements Stapel<T>{
private class Item {
public Item next;
public T value;
public T value1;
public T value2;
}
private Item first;
private Item last;
private Item pre;
public boolean isEmpty() {
return first == null;
}
public void push(T e, T e1, T e2)
{
Item item = new Item () ;
item.next = null;
item.value = e ;
item.value1 = e1;
item.value2 = e2;
if (isEmpty()) {
first = item;
first.next = null;
}
else {
pre = first;
first = item;
first.next = pre;
}
}
public T peek() {
if (isEmpty()) {
throw new java.util.NoSuchElementException();
}
return first.value;
}
public T peek1() {
if (isEmpty()) {
throw new java.util.NoSuchElementException();
}
return first.value1;
}
public T peek2() {
if (isEmpty()) {
throw new java.util.NoSuchElementException();
}
return first.value2;
}
public void pop() {
if (isEmpty()) {
throw new java.util.NoSuchElementException();
}
first = first.next;
}
}
public类EVLStapel实现statpel{
私人类项目{
其次是公共项目;
公共价值观;
公共价值1;
公共价值2;
}
私人物品优先;
私人物品最后;
私人物品预处理;
公共布尔值为空(){
返回first==null;
}
公共无效推送(TE、e1、e2)
{
项目=新项目();
item.next=null;
项目价值=e;
第1项数值=e1;
项目1.2=e2;
if(isEmpty()){
第一个=项目;
first.next=null;
}
否则{
前=第一;
第一个=项目;
first.next=pre;
}
}
公共T peek(){
if(isEmpty()){
抛出新的java.util.NoSuchElementException();
}
首先返回值;
}
公共交通1({
if(isEmpty()){
抛出新的java.util.NoSuchElementException();
}
首先返回值1;
}
公共电话号码(2){
if(isEmpty()){
抛出新的java.util.NoSuchElementException();
}
首先返回值2;
}
公共空间{
if(isEmpty()){
抛出新的java.util.NoSuchElementException();
}
first=first.next;
}
}
那么,哪部分不起作用,以何种方式?在Showall类中使用Reader类中的堆栈。我得到一个空指针异常。我想做的是,在Reader类中的case字符串中创建一个对象堆栈。稍后我想在showall类中的init()方法中使用这个堆栈。如果要在活动之间共享对象,通常效果不太好。最好将其打包或序列化,并通过Intent从一个活动传递到下一个活动。这是您自己编写的堆栈类还是从库中编写的堆栈类?它应该在f.e.下运行。它工作得非常好:public static void main(字符串args[]){init();}public static void init(){Reader r=new Reader();r.add();while(r.s.isEmpty()=false){System.out.println(r.s.peek());System.out.println(r.s.peek());System.out.println(r.s.peek2());r.s.pop();}}当然,如果一切都是静态的,那么你就没事了。这些是静态方法吗?
public class Reader extends ActionBarActivity {
Stapel<String> s = new EVLStapel<String>();
public void add(){
AssetManager mctr;
String zeile = null;
try{
mctr = getAssets();
InputStream is = mctr.open("ausgabe.txt");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while((zeile = br.readLine()) != null)
{
String[] parts = zeile.split(",");
s.push(parts[0], parts[1], parts[2]);
zeile = br.readLine();
}
br.close();
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Showall extends ActionBarActivity implements OnClickListener {
Reader r = new Reader();
...
while(s.isEmpty() == false)
{
TableRow row = new TableRow(this);
TextView p = new TextView(this);
p.setText(r.s.peek());
row.addView(p);
TextView sp = new TextView(this);
sp.setText(r.s.peek1());
row.addView(sp);
TextView pp = new TextView(this);
pp.setText(r.s.peek2());
row.addView(pp);
table.addView(row);
r.s.pop();
}
public class EVLStapel<T> implements Stapel<T>{
private class Item {
public Item next;
public T value;
public T value1;
public T value2;
}
private Item first;
private Item last;
private Item pre;
public boolean isEmpty() {
return first == null;
}
public void push(T e, T e1, T e2)
{
Item item = new Item () ;
item.next = null;
item.value = e ;
item.value1 = e1;
item.value2 = e2;
if (isEmpty()) {
first = item;
first.next = null;
}
else {
pre = first;
first = item;
first.next = pre;
}
}
public T peek() {
if (isEmpty()) {
throw new java.util.NoSuchElementException();
}
return first.value;
}
public T peek1() {
if (isEmpty()) {
throw new java.util.NoSuchElementException();
}
return first.value1;
}
public T peek2() {
if (isEmpty()) {
throw new java.util.NoSuchElementException();
}
return first.value2;
}
public void pop() {
if (isEmpty()) {
throw new java.util.NoSuchElementException();
}
first = first.next;
}
}