0

In my previous question I found out how to print each array of a 2d array, now I finished this code below. I'd like to know if this is one / the fastest way of doing it? This should be in linear time because we have only if-statements and not nested for-loops (?)

If it's not, please tell me what would be the most efficient way?

public class Print{
    public static void main(String[] args){

        int[][] myArray = {{1,2,3,4},{5,6,7,8}};

        int i=0;
        System.out.print("[");
        for(int j=0; j<myArray[0].length; j++){
            System.out.print(myArray[i][j]);
            if(j != myArray[0].length-1){
                System.out.print(", ");
            }
            if(j == myArray[0].length-1){
                System.out.print("], ");
            }
        }
        int k=1;
        System.out.print("[");
        for(int j=0; j<myArray[1].length; j++){
            System.out.print(myArray[k][j]);
            if(j != myArray[1].length-1){
                System.out.print(", ");
            }
            if(j == myArray[1].length-1){
                System.out.print("]");
            }
        }   
    }
}

Output: [1, 2, 3, 4], [5, 6, 7, 8]

Edit: I'm not looking for a way to code this shorter. I'm rather looking for the speed of algorithm, the fastest.

  • 1
    Here's an easier way: `Arrays.deepToString(myArray)` – shmosel Dec 23 '16 at 21:04
  • What does this do? There is a function behind it? Didn't know I will test it now. Btw I'm not asking for a shorter way to code it but faster algorithm. –  Dec 23 '16 at 21:04
  • http://stackoverflow.com/q/19648240/4821941 – m3kkis Dec 23 '16 at 21:07
  • @m3kkis I have seen that question before I asked this one but it didn't help because nested for-loops were used. –  Dec 23 '16 at 21:09
  • @cnmesr And you think nested `for` loops are worse than your 2 `for` loops which wouldn't even work if you had an array with more than two rows? – Tom Dec 23 '16 at 21:11
  • In this question I only concentrate on 2d arrays. And I think 2 single for-loops are faster than nested. Or not? –  Dec 23 '16 at 21:12
  • 2
    So you don't know what 2d arrays are, ok. 2d stands for 2 dimensional, not for 2 rows of arbitrary length, so 3x3 is also a 2d array. And no, nested loops aren't faster if you have a matching number of `for` loops copied one after another. Nested loops are just cleaner, as you don't need to copy your code and they can handle an arbitrary size of your arrays, even a 1d one. – Tom Dec 23 '16 at 21:18
  • Ohh very good to know thanks Tom –  Dec 23 '16 at 21:19
  • Also mind that Java doesn't have "real" 2d arrays, it has arrays of array (or arrays of arrays of array, depending on the dimensions). So this `{{1, 2, 3}, {1}, {1, 2, 3, 4, 5, 6, 7, 8, 9}}` is valid here and your code should be able to deal with these "jacked arrays", or you'll risk an `ArrayIndexOutOfBoundsException`. – Tom Dec 23 '16 at 21:23
  • The speed will almost entirely be constrained by the number of characters/lines you display on the screen. How you produce those characters is unlikely to matter much. – Peter Lawrey Dec 23 '16 at 21:54

1 Answers1

1

You can easily use the code like this:

int[][] myArray = {{1,2,3,4},{5,6,7,8}};
System.out.print(Arrays.deepToString(myArray));

It will be a lot faster.

Ihor Dobrovolskyi
  • 1,241
  • 9
  • 19
  • *"It will be a lot faster."* And why do you think that? – Tom Dec 23 '16 at 21:12
  • I still don't know what's behind this short code. There must be a (long?) code behind it and I'm not sure if it's faster or slower than the one I posted? Because this is defined in software like eclipse. –  Dec 23 '16 at 21:14
  • Set your cursor on this method and press "F3". – Ihor Dobrovolskyi Dec 23 '16 at 21:16