Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: prininting out a 4x4 array according to an order by a 1d array

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default prininting out a 4x4 array according to an order by a 1d array

    Hi Guys,

    I know i have posted quiyte a bit in the past couple of days and really greatful for all the help but i have hit a dead end again and been trying to figure it out for a few days now.

    Right this is what i am trying to achive


    I have a 2d array (blockarray[][]) which contains bytes of data and what i am trying to do is put each colomn into a String via a specic order which is set by the1d array (int[] colprintorder = new int[numberofcols]; ) which is the length of the 2d array containing the numbers 0-the number of coloumns in the array. This is the order i want each coloumn to be printed out (i know the order makes no sense its part of a transposition cipher thing).



    So if you have a look at the photo above it may help you undfer stand what im trying to do . the black text is the byte array of the data, the orange array is the list i want each colom of the black array put into a single string. the green arrow is just demonstarating that im trying to print out the column and not row. So coloumn 8 (the one with the orange 0 above) will be printed out first then column 9 and so on till all the columns have been printed



    you see the last 10 entrys are "21" i want these to be ignored so i tried creating a if between 32-177 (this range is beacuse it equals all usable characters on a english keyboard).


    here is the code i have produced to try and do this

    boolean end = false;
     
    int mewo=0;
    while(end==false)
    {
        if(her==colprintorder.length)
        {
            end = true;
        }
     
     
        for (int posloop=0; posloop <colprintorder.length; posloop++)
        {
     
            if(colprintorder[posloop]==mewo)
            {
                 for (int b=0; b<4;b++)
                 {
     
     
                     cipherchararray[her] = (char)(byte) BlockArray[b][mewo];
     
                 }
                her++;
            }
     
        }
     
     
    }
     
    String covertedChars = new String(cipherchararray);


    at the moment im getting no where can anyone help me figure this out please .. thank you


  2. #2
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: prininting out a 4x4 array according to an order by a 1d array

    i have used various combinations of whiul loops, ifs and fors but still not getting anywhere

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,264
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: prininting out a 4x4 array according to an order by a 1d array

    If I understand the algorithm correctly:
    1) Create a TreeMap, keyed with the value of the columntorprint, and valued with its index in the coltoprint array. This serves as a lookup table and will order the keys for you.
    2) Get its keySet and loop over its iterator(which will be the ordered values of the coltoprint) and get the corresponding value, which is the actual column that you can use to lookup in your 4x4 array
    3) Using the above, you can print out the array by column (all columns at once), or by row (loop over each row and use the TreeMap to get the appropriate value)
    EDIT: you don't necessarily need a treemap for this, as you can use another array as a lookup table.
    Last edited by copeg; November 23rd, 2010 at 05:34 PM.

  4. #4
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: prininting out a 4x4 array according to an order by a 1d array

    i have never used tree maps before so ill do some research thanks for your input i will let you know how i get on
    Last edited by fortune2k; November 23rd, 2010 at 07:02 PM.

  5. #5
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: prininting out a 4x4 array according to an order by a 1d array

    right guys i have been playing around with tree mapping, i dont really know whats happening , ive got my code stuck im trying to add each coloumn to the 1d char array. Could you please steer me in the right direction. Thanks

    String aaa ="";
     
    TreeMap <Integer, Byte>tMap = new TreeMap<Integer, Byte>();
    TreeMap <Integer, Byte>tMap1 = new TreeMap<Integer, Byte>();
    TreeMap <Integer, Byte>tMap2 = new TreeMap<Integer, Byte>();
    TreeMap <Integer, Byte>tMap3 = new TreeMap<Integer, Byte>();
     
     
     
    Object heee;
    String lolcat= null;
     
     
     
    byte[] cipherf = new byte[4*numberofcols];
     
     
    for(int i=0; i< colprintorder.length; i++)
    {
    tMap.put(colprintorder[i],BlockArray[0][i]);
    tMap1.put(colprintorder[i],BlockArray[1][i]);
    tMap2.put(colprintorder[i],BlockArray[2][i]);
    tMap3.put(colprintorder[i],BlockArray[3][i]);
    }
    System.out.print("\n\n");
     
    System.out.println("Values of tree map: " + tMap.values() );
    System.out.println("Values of tree map: " + tMap1.values());
    System.out.println("Values of tree map: " + tMap2.values());
    System.out.println("Values of tree map: " + tMap3.values());
     
    System.out.println("size: " +tMap.size());
     
     
     
     
      for(int a=0;a<tMap.size();a++)
      {
        cipherf[a] = tMap.get(a);  
     
        System.out.println("cipherf @" +a+" = "+ tMap.get(a)); 
     
      }
     
      for(int a=0;a<4;a++)
      {
        cipherf[a] += tMap.get(a);
        cipherf[a+1]= tMap1.get(a);
        cipherf[a+2] = tMap2.get(a);
        cipherf[a+3] = tMap3.get(a);
      }
     
    for(int a=0; a<cipherf.length; a++)
    {
        System.out.printf("%4s",cipherf[a]);
    }


    my output is:



    You can see that the array at the bottom is now in order by the column print order.


    i need to somehow deal with 1 treemap instead of 4 for each row I also need to put each of the 4 bits in each column into a 1d byte array from right to left (no certain order). For example in the testing output the data in the greenbox first then the orange box also would like to tidy the code up i am just messing around at the mo.

    cheers and good night

  6. #6
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: prininting out a 4x4 array according to an order by a 1d array

    hi guys i have been playing arrounbd with the code above and still not getting anywhere. has anyone know of any good tutorials about tree mapping so i can figure this out thanks
    Last edited by fortune2k; November 24th, 2010 at 06:50 PM.

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,264
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: prininting out a 4x4 array according to an order by a 1d array

    Looks like you are on the right track. You could just have a single treemap specify the index in the array, and not necessarily the value at that index. So for example
    Map<Integer, Integer> lookup = new TreeMap<Integer,Integer>();
    for ( int i = 0; i < colprintorder.length; i++ ){
        lookup.put(colprintorder[i], i);
    }
    //now iterate over each row of block array, see code example below

    As I mentioned above, you could also do this with just an array
    int[] lookup = new int[colprintorder.length];
    for ( int i = 0; i < colprintorder.length; i++ ){
        lookup[colprintorder[i]] = i;
    }
    //then like above, do the math to find the corresponding location in the blockarray...pseudocode
    for each row:
        for each value in row:
            nextvalue = blockarray[rownumber * rowlength + lookup[colnumber]];
    I can't guarantee the validity of the math of the above pseudo-code, but hopefully its enough to get you pointed in the right direction.

  8. #8
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: prininting out a 4x4 array according to an order by a 1d array

    i think the best way is to use treemaps because it orders them ect how i need the array i just need to find a way which i dont need 4 serpearte treemaps for each row and a way to convert the treemap to an array which i can print out each coloum in a string (greenbox then orange box and so on

Similar Threads

  1. Char array to a String array with hex
    By fortune2k in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2014, 12:01 PM
  2. 2d (4x4) array insdie a 1d array. (Block cipher)
    By fortune2k in forum Collections and Generics
    Replies: 13
    Last Post: November 23rd, 2010, 04:29 PM
  3. Replies: 4
    Last Post: November 14th, 2010, 10:44 AM
  4. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 04:09 AM
  5. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM