简体   繁体   English

访问不同类中的数组列表

[英]Access array list in a different class

Since this is a re-attack on my first question, I will begin by restating that I am a java newbie. 由于这是对我的第一个问题的重新攻击,因此我将从重申我是Java新手开始。 I am also trying to work through a college level java assignment for this week. 我也在本周尝试完成大学级别的Java作业。 To caveat this, let me say I barely know what I am doing. 为了说明这一点,让我说我几乎不知道自己在做什么。 The assignment this week after getting clarification; 澄清后,本周的作业; is simply this: 简直是这样:

You need to write an actual ClassClient for this project that: 您需要为此项目编写一个实际的ClassClient:

  1. declared and initializes your data array 声明并初始化您的数据数组
  2. creates a SortingClass object, 创建一个SortingClass对象,
  3. prints the array of data (unsorted) 打印数据数组(未排序)
  4. sends the array into the SortingClass's constructor (which sorts the contents of the array) 将数组发送到SortingClass的构造函数(对数组的内容进行排序)
  5. prints the same array of data (now sorted) 打印相同的数据数组(现已排序)

As of now, I am working through it set step by step. 到目前为止,我正在逐步解决它。 Currently, the only error NetBeans is giving me is that in my second class, everywhere array1 is used is underlined and I assume it means they are not linking to the first class where the array list is placed. 当前,NetBeans给我的唯一错误是在我的第二个类中,凡是使用array1的地方都带有下划线,并且我认为这意味着它们没有链接到放置数组列表的第一个类。 MY first question is, I'm obviously not linking correctly; 我的第一个问题是,我的链接显然不正确; what am I missing? 我想念什么? I am also using the selection sort method. 我也在使用选择排序方法。

My first class called ClientClass 我的第一堂课叫做ClientClass

public class ClientClass {

    public static void main( double [] array ) {

        double array1[] =
        {53.5, 60.3, 96.2, 53.3, 56.4, 52.7, 76.4, 77.5, 71.0, 78.2,
        65.2, 59.3, 80.5, 92.1, 85.7, 78.7, 66.2, 88.8, 50.2, 73.4};

        //Create a SortingClass variable with data gievn in the array
        SortingClass g = new SortingClass();

        // Print array unsorted
        for (double number : array1) 
        {
        System.out.println("Number = " + number);
        }

        String outPutString = g.toString();
        System.out.println(outPutString);
    }

}

and I have written the second class 我已经写了第二节课

public class SortingClass {

    // Selection Sort
    public static void ClientClass (double [] array )
    {
        double temp;
        int max;

        // Selection Sort Method
        for (int i = 0; i < array1.length - 1; i ++)
        {
            max = indexOfLargestElement ( array1, array1.length - i );

            temp = array1[max];
            array1[max] = array1[array1.length - i - 1];
            array1[array1.length - i - 1] = temp;
        }
    }

    public static int indexOfLargestElement ( double[] array1, int size)
    {
        int index = 0;
        for ( int i = 1; i < size; i++ )
        {
            if ( array1[i] > array1[index] )
                index = i;
        }
        return index;
    }
}

Your very first problem is that this 您的第一个问题是

sends the array into the SortingClass's constructor (which sorts the contents of the array) 将数组发送到SortingClass的构造函数(对数组的内容进行排序)

translates to: you can't be using static all over the place! 转换为:您不能在所有地方都使用static

You need something like 你需要类似的东西

public class SortingClass {
  private final double data[];
  public SortingClass(double data[])  {
    this.data = data;
  }
  public void sort() {
    ... would sort on this.data

Meaning: you create an instance of that class, and you pass a reference to that array into that class. 含义:创建该类的实例,然后将对该数组的引用传递给该类。

As a starter, you would want to study stuff like this . 作为首发,你想学习的东西像这样

public static void ClientClass (double [] array ) {} this method is taking an array of doubles called "array" as an argument. public static void ClientClass (double [] array ) {}此方法将一个名为“ array”的双精度数组作为参数。 When you try and reference "array1" inside this method, it has no idea what you're talking about because there is no such variable created in the scope. 当您尝试在此方法中引用“ array1”时,它不知道您在说什么,因为在范围内没有创建此类变量。

If you change this public static void ClientClass (double [] array ) {} to this public static void ClientClass (double [] array1 ) {} the method will then understand that every time you reference "array1", you are talking about the array you passed in as the method parameter. 如果将此public static void ClientClass (double [] array ) {}更改为此public static void ClientClass (double [] array1 ) {}该方法将理解为每次引用“ array1”时,您都在谈论数组您作为方法参数传入。

As long as the methods in SortingClass stay static, its enough to just call it from the main class. 只要SortingClass中的方法保持静态,就足以从主类中调用它。 There is not need to instantiate SortingClass by a "new": 无需通过“新”实例化SortingClass:

SortingClass.ClientClass( array1);

BTW1: Good naming preserves you and others from headache. BTW1:好的命名可以使您和其他人免受头痛。 Eg methods ("ClientClass") first start with lower letter, and second, the name suggests that it is a class, but it is a (void) method which performs a sorting. 例如,方法(“ ClientClass”)首先以小写字母开头,其次,名称表明它是一个类,但它是执行排序的(无效)方法。 You better think that methods are verbs, whereas classes/instances are nouns. 您最好认为方法是动词,而类/实例是名词。

BTW2: Your main method - if you want to start this as a java application - should take a String array and not a double array. BTW2:您的主要方法-如果要作为Java应用程序启动它-应该采用String数组而不是double数组。

Thanks everyone. 感谢大家。 The initial error question has been resolved and my first class is working. 最初的错误问题已解决,我的第一堂课正在工作。 For my second class (SortingClass), It is not however and I believe that I need to completely redo it. 对于我的第二堂课(SortingClass),不是,我相信我需要完全重做。 I am going to get to work on it right now. 我现在要开始研究它。 Thanks again. 再次感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM