0

I have a single demenational array with student names and a 2D array with student marks, I can sort the names fine but I cannot get the marks to match (as they have to stay in the same order). Here is my attempted code:

static String[] studentNamesArray = new String[10];
static int[][] studentMarksArray = new int[10][3];

  static void sortAlphabetical() { 
    String tempName;
    int intSwap;
    boolean flag = false;
    while (flag==false) {
      flag = true;
      for (int i = 0; i < 9; i++) {
        if (studentNamesArray[i].compareTo(studentNamesArray[i + 1])>0) {
          tempName = studentNamesArray[i];
          studentNamesArray[i] = studentNamesArray[i + 1];
          studentNamesArray[i + 1] = tempName;

          for(int y=0;y<2;y++){
          intSwap = studentMarksArray[i][0];
          studentMarksArray[i][y] = studentMarksArray[i+1][y+1];
          studentMarksArray[i+1][y+1] = intSwap;
          }
          flag = false;
        }
      }
    }
  }
4
  • 1
    Any reasons that you don't want to create a Student object holding its name and its marks, then create a Student[] array and sort it by name using a Comparator ? Commented Nov 28, 2013 at 15:58
  • I'm limited to non OO methods Commented Nov 28, 2013 at 15:59
  • compareTo is a OO method... Commented Nov 28, 2013 at 16:07
  • What I meant was I could not have have separate class files Commented Nov 28, 2013 at 16:11

1 Answer 1

3

You just have to swap the marks arrays like you did for the names :

static void sortAlphabetical() { 
        String tempName;
        int [] intSwap; //<-- note I changed this as an int[] array
        boolean flag = false;
        while (flag==false) {
          flag = true;
          for (int i = 0; i < studentNamesArray.length-1; i++) { //<-- note I changed this to length - 1 to avoid IndexOutOfBoundsException
            if (studentNamesArray[i].compareTo(studentNamesArray[i + 1])>0) {
              tempName = studentNamesArray[i];
              studentNamesArray[i] = studentNamesArray[i + 1];
              studentNamesArray[i + 1] = tempName;

              intSwap = studentMarksArray[i];
              studentMarksArray[i] = studentMarksArray[i+1];
              studentMarksArray[i+1]= intSwap;
              flag = false;
            }
          }
        }
      }

    static String[] studentNamesArray = new String[3];
    static int[][] studentMarksArray = new int[3][3];

    public static void main (String[] args) throws java.lang.Exception {
        studentNamesArray[0] = "Mark";
        studentNamesArray[1] = "Anna";
        studentNamesArray[2] = "Arnold";

        studentMarksArray[0] = new int[]{1,2,3};
        studentMarksArray[1] = new int[]{4,5,6};
        studentMarksArray[2] = new int[]{0,0,0};

        sortAlphabetical();
        System.out.println(Arrays.toString(studentNamesArray));
        System.out.println(Arrays.deepToString(studentMarksArray));

    }

Output :

[Anna, Arnold, Mark]
[[4, 5, 6], [0, 0, 0], [1, 2, 3]]
Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain why you changed intSwap to an array?
@Colin747 Because initially each mark array is associated as the same index position as the String array right ? So if you swap two names in the array, you have too swap their corresponding mark arrays too.
I wasn't questioning why you did it, I just wanted to make sure I understood correctly what was going on, your solution worked great, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.