从主类外包堆栈和文件读取器

时间:2015-02-27 17:01:07

标签: java android stack

您好我有点失落,可以使用一些帮助。 所以基本上我有一堆对象,在这种情况下是字符串。

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();


        }

我正在运行add()方法,它只是读取文本文件并将具有3个值的对象推入我的堆栈 &安培; init()方法,它基本上将它们从堆栈中拉出来:

public void add(){
    AssetManager mngr;
    String line = null;


    try{
        mngr = getAssets();
        InputStream is = mngr.open("ausgabe.txt");
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        while((line = br.readLine()) != null)
        {
            String[] parts = line.split(",");
                s.push(parts[0], parts[1], parts[2]);





        }
        br.close();
    }catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   


}

这是init()的重要部分:

 while(s.isEmpty() == false)
    {
    TableRow row = new TableRow(this);
    TextView p = new TextView(this);
     p.setText(s.peek());

       row.addView(p);

       TextView sp = new TextView(this);
       sp.setText(s.peek1());
       row.addView(sp);

       TextView pp = new TextView(this);
       pp.setText(s.peek2());
       row.addView(pp);
       table.addView(row);
       s.pop();
   }

所以现在我的问题。我想外包我的堆栈和add方法因为,我将在许多不同的类中使用这个方法,如果我可以为我的堆栈和我的文件读取器创建一个单独的类,那将会少得多。 但我不知道怎么做。 我试过像:

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();
       }

编辑: 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;



      }

    }

0 个答案:

没有答案