2 Replies Latest reply: Aug 27, 2009 4:04 AM by jduprez RSS

    How to implent "X not yet an element of array[][] on row r"?

    843853
      Hi, I'm a student business engineering and we have to write a program to solve a certain logistic problem. I still have a problem though and I can't seem to figer out: how do you implement the statement "X not yet an element of array[][] on row r of that array"?

      To put thing into context, here is my code (in bold and underlined is where my problem is):

      public int [][] calcSolutions (){

      int k = 0;
      int c = 1;

      for (int i = 0; i < nrJobs; ++i){
      .....setupTimeSol[0] = initSetupTime[i];
      .....seqJobsSol[i][0] = i;
      .....int r = i;
      .....int s = setupTime[r][0];
      .....while (c < nrJobs){
      ..............for (int m = 1; m < nrJobs; ++m){
      ....................if (setupTime[r][0] > setupTime[r][m] && "*_m not yet an element of seqJobsSol[] on row r_*"){
      ........................s = setupTime[r][m];
      ........................k = m;
      ....................}
      ....................setupTimeSol[r][c] = s;
      ....................seqJobsSol[r][] = k;
      ....................++c
      ....................}
      ........... r = k;
      ............}
      .......}
      }

      I have already posted another thread where I explain my problem more elaborate (Implementation of Nearest-neighbour algorithm (single machine schedueling))

      PLEASE HELP

      Edited by: mdcnuydt on Aug 21, 2009 12:29 PM
        • 1. Re: How to implent "X not yet an element of array[][] on row r"?
          843853
          mdcnuydt wrote:
          m not yet an element of seqJobsSol[] on row r
          Can this be translated as : m not in seqJobsSol[].

          If this is so, you might find using a utility method in java.util.Arrays helpful.

          Or convert the array to a list and use if List.contains(m) != true
          • 2. Re: How to implent "X not yet an element of array[][] on row r"?
            jduprez
            Don't expect too much of my answer, as I'm not sure I understand your need - I think your wording (or the exercise's wording) is ambiguous.
            I understand the statement as "in the original 2D-array, X is not yet an element of the 1D-array of columns obtained when I fix the index of the "row" dimension to r".
            how do you implement the statement "X not yet an element of array[][] on row r of that array"?
            seqJobsSol is apparently (as per the pcode extract) an int[][], that is, an array of int of two dimensions. In Java (contrary to C), that translates as an array (1-dimensional) of (1-dimensional) arrays of int.
            That is, for each valid index i, +seqJobsSol[i]+ is an int[] (array of int).

            Therefore, I understand the statement as "X not yet an element of array[][] on row r of that array" as "X is not in seqJobsSol[r]".
            Assuming r is a valid index for the first dimension, checking the conditions means iterating over all ints in +seqJobsSol[r]+ (which itself is an int[]).

            In terms of code:
            public int[](] calcSolution() {
                ....
                if (setupTime[r][0] > setupTime[r][m] && doesntContain(seqJobsSol[r], m){
                    s = setupTime[r][m];
                    k = m;
                }
                ...
            }
            
            private boolean doesntContain(int[] oneDIntArray, int searchedValue) {
                boolean found = false;
                for (int i=0; i<oneDIntArray.length; i++) {
                    int value = oneDIntArray;
            if (value==searchedValue) {
            found = true;
            break;
            }
            return found;
            }
            Note that you can write the for loop using the new foreach syntax that masks the +i+, but I wanted to make index handling and dimensions explicit.
            for (int value: oneDIntArray) {...}
            I have already posted another thread where I explain my problem more elaborate (Implementation of Nearest-neighbour algorithm (single machine schedueling))
            You should link on that thread, to give a better understanding of what you want (again, the wording is not so clear). Note: please format code you post using code tags. There's also a CODE button in the post editor to do that automatically (just paste the code, select all of it, and press the CODE button). Edited by: jduprez on Aug 27, 2009 9:00 AM