简体   繁体   中英

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. I am also trying to work through a college level java assignment for this week. 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:

  1. declared and initializes your data array
  2. creates a SortingClass object,
  3. prints the array of data (unsorted)
  4. sends the array into the SortingClass's constructor (which sorts the contents of the array)
  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. 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

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)

translates to: you can't be using static all over the place!

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. 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.

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.

As long as the methods in SortingClass stay static, its enough to just call it from the main class. There is not need to instantiate SortingClass by a "new":

SortingClass.ClientClass( array1);

BTW1: Good naming preserves you and others from headache. 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. 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.

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. I am going to get to work on it right now. Thanks again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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