Java和ArrayLists

时间:2018-03-26 13:28:10

标签: java arraylist

    package test.arraylist;

    import java.util.Scanner;

    public class TestArraylist {
    static Scanner keyboard = new Scanner (System.in);
    static int how_many;  
        public static void main(String[] args) {
            menu();
        }
        public static void menu()
        {
            System.out.println("1.L2C");
            System.out.println("2.M2R");

            int menu = keyboard.nextInt();keyboard.nextLine();
            switch (menu){
                case 1:

                    NewClass l2c = new NewClass();
                    System.out.println("How many:");
                    how_many = keyboard.nextInt();keyboard.nextLine();
                    for (int i=0;i<how_many;i++){
                    System.out.println("Insert values:");
                    l2c.l2c.add(keyboard.nextLine());
                    System.out.println("values:"+l2c.getL2c());
                    }
                    break;
                case 2:

                    NewClass r2p = new NewClass();
                    System.out.println("How many:");
                    how_many = keyboard.nextInt();keyboard.nextLine();
                    for (int i=0;i<how_many;i++){
                    System.out.println("Insert values:");
                    r2p.r2p.add(keyboard.nextLine());
                    System.out.println("values:"+r2p.getR2p());
                    }
                    break;


            }


        }
    public static void seats(){
        System.out.println("values:"+r2p.getR2p()); // Error
        System.out.println("values:"+l2p.getR2p()); // Error 
    }

    }

--------------------------- CLASS ---------------- -------

package test.arraylist;

import java.util.ArrayList;
public class NewClass {

      ArrayList<String> l2c = new ArrayList<>();
      ArrayList<String> r2p = new ArrayList<>();


    public ArrayList<String> getL2c() {
        return l2c;
    }

    public void setL2c(ArrayList<String> l2c) {
        this.l2c = l2c;
    }

    public ArrayList<String> getR2p() {
        return r2p;
    }

    public void setR2p(ArrayList<String> r2p) {
        this.r2p = r2p;
    }



}

我在java只用了2个星期 简而言之,我将解释我正在尝试做什么。

此用户进入后,我正在创建ArrayList 价值为Arraylist
一切都完成后,我想从另一个方法访问数据并进行一些数学运算,我意识到我应该使用某种返回,我试图这样做,但它不起作用。

我肯定做错了。请帮忙解决这个问题。我花了2天时间。

2 个答案:

答案 0 :(得分:0)

如果您调用seats(),则会出现错误,因为这些变量未在适当的范围内声明。要在该方法中使r2pl2c可用,请使用初始值设定项main()将变量声明移到方法之外(在类声明之下,private static NewClass newClass;之前)。随意根据需要分配值,或使用可选项(但考虑到你只使用Java 2周,远离可选项;只需确保分配值)。

编辑:对变量使用static修饰符,因为您是从静态上下文访问的。

答案 1 :(得分:0)

你需要的是你需要的NewClass组成你的主类,即。TestArraylist,阅读java中的组成composition-in-java-example

现在你的代码应该是这样的,

  public class TestArraylist {
   static NewClass newClass = new NewClass(); 
 ---------
 --------
     public static void menu(){

            case 1:
                ArrayList<String> l2c = newClass.getL2c(); //this way you can access the data
                System.out.println("How many:");
                how_many = keyboard.nextInt();keyboard.nextLine();
                for (int i=0;i<how_many;i++){
                System.out.println("Insert values:");
                l2c.add(keyboard.nextLine()); //here you need only the instance variable to add the data into the arraylist.
                System.out.println("values:"+l2c.toString()); //to print the data
                }
                break;
    //Same way you should modify the case2 as well.
            case 2:

                ArrayList<String> r2p = newClass.getR2p();
                System.out.println("How many:");
                how_many = keyboard.nextInt();keyboard.nextLine();
                for (int i=0;i<how_many;i++){
                System.out.println("Insert values:");
                r2p.add(keyboard.nextLine());
                System.out.println("values:"+r2p.toString());
                }
                break;

现在,在下面的方法中可以访问复合对象,因为我们已将其声明为静态。

public static void seats(){ System.out.println("values:"+newClass.getR2p()); // not throw any error System.out.println("values:"+newClass.getR2p()); // not throw any error }

此外,Newclass应该像这样定义,

public class NewClass {
private ArrayList<String> l2c = new ArrayList<String>();
private ArrayList<String> r2p = new ArrayList<String>();

public ArrayList<String> getL2c() {
    return l2c;
}

public void setL2c(ArrayList<String> l2c) {
    this.l2c = l2c;
}

public ArrayList<String> getR2p() {
    return r2p;
}

public void setR2p(ArrayList<String> r2p) {
    this.r2p = r2p;
}
}

有许多事情需要纠正,但到目前为止,要使你的程序正常工作,这应该足够了。