Joseph B Howle Joseph B Howle - 1 year ago 73
Java Question

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


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;

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download