如何将数组从一种方法打印到另一种方法?

How to print an array from one method to another?

我需要一种方法来打印 3 个这些数组...

public static void viewCatalog(){
    String[] description = new String[15];
    description[0]= "Alumni Drink ware";
    description[1]= "Binders";
    description[2]= "Bookbag";
    description[3]= "Fabulous Desserts";
    description[4]="Folders";       
    description[5]="Gift Cards";        
    description[6]="Highlighters";
    description[7]="Jackets";
    description[8]="JAVA Programming";
    description[9]="Network Solutions";
    description[10]="Pencils";        
    description[11]="Pens";
    description[12]="Shorts";
    description[13]="Sweatshirts";
    description[14]="Tshirts";
    description[15]="Web Design Ideas";


    String[] category = new String[15];
    category[0]= "Gifts";
    category[1]= "School Supplies";
    category[2]= "School Supplies";
    category[3]= "Textbooks";
    category[4]="School Supplies";       
    category[5]="Gifts";        
    category[6]="School Supplies";
    category[7]="Campus Gear";
    category[8]="Textbooks";
    category[9]="Textbooks";
    category[10]="School Supplies";        
    category[11]="School Supplies";
    category[12]="Campus Gear";
    category[13]="Campus Gear";
    category[14]="Campus Gear";
    category[15]="Textbooks";

    double[] price = new double[15];
    price[0]= 25.00;
    price[1]= 3.00;
    price[2]= 20.00;
    price[3]= 25.00;
    price[4]=1.00;       
    price[5]=25.00;        
    price[6]=2.00;
    price[7]=65.00;
    price[8]=150.00;
    price[9]=75.00;
    price[10]=1.00;        
    price[11]=2.00;
    price[12]=10.00;
    price[13]=40.00;
    price[14]=15.00;
    price[15]=55.00;

    System.out.println(Arrays.toString(description));
    System.out.println(Arrays.toString(category));
    System.out.println(Arrays.toString(price));
}

在这个方法中...

public static void sort(){
    System.out.println("How would you like to sort?");
    System.out.println("a) Increasing Price \n"
            + "b) Decreasing Price \n"
            + "c) Description \n"
            + "d) Category \n"
            + "Option: ");

    Scanner input = new Scanner(System.in); 
    String option=input.next();

    if (option=="a") {



    }

}

我需要能够按不同的顺序打印它们,但现在我根本无法打印它们。我不断收到 "class not found" 等错误。我尝试按值传递,但我认为我做的不对。请帮忙!我无论如何都不擅长 Java,需要我能得到的所有帮助。

viewCatalog() 方法中取出数组的声明(在解决 Jaime 在评论中提到的数组长度问题之后)。继续以该方法填充它们,只需取出您声明它们的位置。然后您可以在 sort 方法中访问 public 静态数组。像这样的东西。

public class Main{

    public static String[] description;
    public static String[] category;
    public static double[] price;

    public static void viewCatalog(){
        //Populate Arrays
        description[0] = "foo";
    }
    public static void sort(){
        //Sort and print
        System.out.println(description[0]);
    }
}

一个可能的解决方案是制作一个 class 并将数组元素存储为字段

clas Item extends Comparable{
   private double price;
   private String desc;
   private String category;

   int sortSelection = (your input here);

   public int compareTo(item i){
       switch(sortSelection){
            case 1: // sort by price
            case 2: // sort by desc
            case 3: // sort by category
       }
   }
}

然后创建一个 Item 的实例数组,每次都简单地调用排序。

这里展示了一个很好的例子 http://examples.javacodegeeks.com/java-basics/java-comparable-example/