How to print array data in columns [duplicate]










0
















This question already has an answer here:



  • Is there an easy way to output two columns to the console in Java?

    3 answers



I have code here that takes in two different array types, one boolean and one of ints. I want to get the code to print like this



marked edgeTo 
------ --------
true 2
true 0
true 0
true 5
true 3
true 0


I am currently using a foreach loop to print out the data and I cannot get edgeTo's data (2 0 0 5 3 0) to print underneath its title edgeTo and across from its respective marked boolean value. Any suggestions as to what I am doing wrong? Here is the snipet of code I have so far



System.out.println("marked edgeTo");
System.out.println("------- --------");

for (boolean el : bob.marked)
System.out.println(el);


for (int el : bob.edgeTo)
System.out.println(el);










share|improve this question















marked as duplicate by DaveyDaveDave, GhostCat java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 8:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    What is marked and edgeTo? --- Anyway, instead of parallel arrays/lists, you should have a single array/list of objects with 2 fields.

    – Andreas
    Nov 13 '18 at 5:13











  • they're public fields of another class within my package I am working on. I am learning about depthfisrt search and marked is checks if there is a path from s-v. and edgeTo is the last on edge on the s-v path.

    – amiej33
    Nov 13 '18 at 5:19















0
















This question already has an answer here:



  • Is there an easy way to output two columns to the console in Java?

    3 answers



I have code here that takes in two different array types, one boolean and one of ints. I want to get the code to print like this



marked edgeTo 
------ --------
true 2
true 0
true 0
true 5
true 3
true 0


I am currently using a foreach loop to print out the data and I cannot get edgeTo's data (2 0 0 5 3 0) to print underneath its title edgeTo and across from its respective marked boolean value. Any suggestions as to what I am doing wrong? Here is the snipet of code I have so far



System.out.println("marked edgeTo");
System.out.println("------- --------");

for (boolean el : bob.marked)
System.out.println(el);


for (int el : bob.edgeTo)
System.out.println(el);










share|improve this question















marked as duplicate by DaveyDaveDave, GhostCat java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 8:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    What is marked and edgeTo? --- Anyway, instead of parallel arrays/lists, you should have a single array/list of objects with 2 fields.

    – Andreas
    Nov 13 '18 at 5:13











  • they're public fields of another class within my package I am working on. I am learning about depthfisrt search and marked is checks if there is a path from s-v. and edgeTo is the last on edge on the s-v path.

    – amiej33
    Nov 13 '18 at 5:19













0












0








0









This question already has an answer here:



  • Is there an easy way to output two columns to the console in Java?

    3 answers



I have code here that takes in two different array types, one boolean and one of ints. I want to get the code to print like this



marked edgeTo 
------ --------
true 2
true 0
true 0
true 5
true 3
true 0


I am currently using a foreach loop to print out the data and I cannot get edgeTo's data (2 0 0 5 3 0) to print underneath its title edgeTo and across from its respective marked boolean value. Any suggestions as to what I am doing wrong? Here is the snipet of code I have so far



System.out.println("marked edgeTo");
System.out.println("------- --------");

for (boolean el : bob.marked)
System.out.println(el);


for (int el : bob.edgeTo)
System.out.println(el);










share|improve this question

















This question already has an answer here:



  • Is there an easy way to output two columns to the console in Java?

    3 answers



I have code here that takes in two different array types, one boolean and one of ints. I want to get the code to print like this



marked edgeTo 
------ --------
true 2
true 0
true 0
true 5
true 3
true 0


I am currently using a foreach loop to print out the data and I cannot get edgeTo's data (2 0 0 5 3 0) to print underneath its title edgeTo and across from its respective marked boolean value. Any suggestions as to what I am doing wrong? Here is the snipet of code I have so far



System.out.println("marked edgeTo");
System.out.println("------- --------");

for (boolean el : bob.marked)
System.out.println(el);


for (int el : bob.edgeTo)
System.out.println(el);





This question already has an answer here:



  • Is there an easy way to output two columns to the console in Java?

    3 answers







java arrays printing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 5:11









Scary Wombat

35k32252




35k32252










asked Nov 13 '18 at 5:09









amiej33amiej33

94




94




marked as duplicate by DaveyDaveDave, GhostCat java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 8:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by DaveyDaveDave, GhostCat java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 8:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 1





    What is marked and edgeTo? --- Anyway, instead of parallel arrays/lists, you should have a single array/list of objects with 2 fields.

    – Andreas
    Nov 13 '18 at 5:13











  • they're public fields of another class within my package I am working on. I am learning about depthfisrt search and marked is checks if there is a path from s-v. and edgeTo is the last on edge on the s-v path.

    – amiej33
    Nov 13 '18 at 5:19












  • 1





    What is marked and edgeTo? --- Anyway, instead of parallel arrays/lists, you should have a single array/list of objects with 2 fields.

    – Andreas
    Nov 13 '18 at 5:13











  • they're public fields of another class within my package I am working on. I am learning about depthfisrt search and marked is checks if there is a path from s-v. and edgeTo is the last on edge on the s-v path.

    – amiej33
    Nov 13 '18 at 5:19







1




1





What is marked and edgeTo? --- Anyway, instead of parallel arrays/lists, you should have a single array/list of objects with 2 fields.

– Andreas
Nov 13 '18 at 5:13





What is marked and edgeTo? --- Anyway, instead of parallel arrays/lists, you should have a single array/list of objects with 2 fields.

– Andreas
Nov 13 '18 at 5:13













they're public fields of another class within my package I am working on. I am learning about depthfisrt search and marked is checks if there is a path from s-v. and edgeTo is the last on edge on the s-v path.

– amiej33
Nov 13 '18 at 5:19





they're public fields of another class within my package I am working on. I am learning about depthfisrt search and marked is checks if there is a path from s-v. and edgeTo is the last on edge on the s-v path.

– amiej33
Nov 13 '18 at 5:19












2 Answers
2






active

oldest

votes


















1














if two arrays have same length, you can use 1 loop



 System.out.println("markedtedgeTo");
System.out.println("-------t------");
for (int i = 0; i < bob.marked.length;i++)
System.out.println(String.valueOf(bob.marked[i]) + "t" + String.valueOf(bob.edgeTo[i]));






share|improve this answer

























  • @Scary I edited.

    – htpvl
    Nov 13 '18 at 5:15






  • 1





    Actually I was meaning the datatypes of bob.marked etc - maybe not an array. I know notice that he does actually say array - my bad.

    – Scary Wombat
    Nov 13 '18 at 5:20







  • 1





    @htpvl thank you so much this worked. I tried a for loop right after I posted my question, and I was very close I had the syntax wrong. Thank you for taking the time to help me

    – amiej33
    Nov 13 '18 at 5:21






  • 1





    @Scary Wombat sorry my apologies yes they are arrays

    – amiej33
    Nov 13 '18 at 5:21


















1














In case your arrays are of different lengths:



System.out.println("marked edgeTo");
System.out.println("------- --------");
int lesserArr = Math.min(bob.marked.length, bob.edgeTo.length);
for (int i = 0; i < lesserArr;i++)
System.out.println(String.valueOf(bob.marked[i]) + "t" + String.valueOf(bob.edgeTo[i]));

for (int j = 0;j < Math.abs(bob.marked.length-bob.edgeTo.length); j++)
if (bob.marked.length < bob.edgeTo.length)
System.out.println("tt" + String.valueOf(bob.edgeTo[lesserArr + j]));
else
System.out.println(String.valueOf(bob.marked[lesserArr + j]));

}





share|improve this answer































    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    if two arrays have same length, you can use 1 loop



     System.out.println("markedtedgeTo");
    System.out.println("-------t------");
    for (int i = 0; i < bob.marked.length;i++)
    System.out.println(String.valueOf(bob.marked[i]) + "t" + String.valueOf(bob.edgeTo[i]));






    share|improve this answer

























    • @Scary I edited.

      – htpvl
      Nov 13 '18 at 5:15






    • 1





      Actually I was meaning the datatypes of bob.marked etc - maybe not an array. I know notice that he does actually say array - my bad.

      – Scary Wombat
      Nov 13 '18 at 5:20







    • 1





      @htpvl thank you so much this worked. I tried a for loop right after I posted my question, and I was very close I had the syntax wrong. Thank you for taking the time to help me

      – amiej33
      Nov 13 '18 at 5:21






    • 1





      @Scary Wombat sorry my apologies yes they are arrays

      – amiej33
      Nov 13 '18 at 5:21















    1














    if two arrays have same length, you can use 1 loop



     System.out.println("markedtedgeTo");
    System.out.println("-------t------");
    for (int i = 0; i < bob.marked.length;i++)
    System.out.println(String.valueOf(bob.marked[i]) + "t" + String.valueOf(bob.edgeTo[i]));






    share|improve this answer

























    • @Scary I edited.

      – htpvl
      Nov 13 '18 at 5:15






    • 1





      Actually I was meaning the datatypes of bob.marked etc - maybe not an array. I know notice that he does actually say array - my bad.

      – Scary Wombat
      Nov 13 '18 at 5:20







    • 1





      @htpvl thank you so much this worked. I tried a for loop right after I posted my question, and I was very close I had the syntax wrong. Thank you for taking the time to help me

      – amiej33
      Nov 13 '18 at 5:21






    • 1





      @Scary Wombat sorry my apologies yes they are arrays

      – amiej33
      Nov 13 '18 at 5:21













    1












    1








    1







    if two arrays have same length, you can use 1 loop



     System.out.println("markedtedgeTo");
    System.out.println("-------t------");
    for (int i = 0; i < bob.marked.length;i++)
    System.out.println(String.valueOf(bob.marked[i]) + "t" + String.valueOf(bob.edgeTo[i]));






    share|improve this answer















    if two arrays have same length, you can use 1 loop



     System.out.println("markedtedgeTo");
    System.out.println("-------t------");
    for (int i = 0; i < bob.marked.length;i++)
    System.out.println(String.valueOf(bob.marked[i]) + "t" + String.valueOf(bob.edgeTo[i]));







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 '18 at 5:13

























    answered Nov 13 '18 at 5:12









    htpvlhtpvl

    62739




    62739












    • @Scary I edited.

      – htpvl
      Nov 13 '18 at 5:15






    • 1





      Actually I was meaning the datatypes of bob.marked etc - maybe not an array. I know notice that he does actually say array - my bad.

      – Scary Wombat
      Nov 13 '18 at 5:20







    • 1





      @htpvl thank you so much this worked. I tried a for loop right after I posted my question, and I was very close I had the syntax wrong. Thank you for taking the time to help me

      – amiej33
      Nov 13 '18 at 5:21






    • 1





      @Scary Wombat sorry my apologies yes they are arrays

      – amiej33
      Nov 13 '18 at 5:21

















    • @Scary I edited.

      – htpvl
      Nov 13 '18 at 5:15






    • 1





      Actually I was meaning the datatypes of bob.marked etc - maybe not an array. I know notice that he does actually say array - my bad.

      – Scary Wombat
      Nov 13 '18 at 5:20







    • 1





      @htpvl thank you so much this worked. I tried a for loop right after I posted my question, and I was very close I had the syntax wrong. Thank you for taking the time to help me

      – amiej33
      Nov 13 '18 at 5:21






    • 1





      @Scary Wombat sorry my apologies yes they are arrays

      – amiej33
      Nov 13 '18 at 5:21
















    @Scary I edited.

    – htpvl
    Nov 13 '18 at 5:15





    @Scary I edited.

    – htpvl
    Nov 13 '18 at 5:15




    1




    1





    Actually I was meaning the datatypes of bob.marked etc - maybe not an array. I know notice that he does actually say array - my bad.

    – Scary Wombat
    Nov 13 '18 at 5:20






    Actually I was meaning the datatypes of bob.marked etc - maybe not an array. I know notice that he does actually say array - my bad.

    – Scary Wombat
    Nov 13 '18 at 5:20





    1




    1





    @htpvl thank you so much this worked. I tried a for loop right after I posted my question, and I was very close I had the syntax wrong. Thank you for taking the time to help me

    – amiej33
    Nov 13 '18 at 5:21





    @htpvl thank you so much this worked. I tried a for loop right after I posted my question, and I was very close I had the syntax wrong. Thank you for taking the time to help me

    – amiej33
    Nov 13 '18 at 5:21




    1




    1





    @Scary Wombat sorry my apologies yes they are arrays

    – amiej33
    Nov 13 '18 at 5:21





    @Scary Wombat sorry my apologies yes they are arrays

    – amiej33
    Nov 13 '18 at 5:21













    1














    In case your arrays are of different lengths:



    System.out.println("marked edgeTo");
    System.out.println("------- --------");
    int lesserArr = Math.min(bob.marked.length, bob.edgeTo.length);
    for (int i = 0; i < lesserArr;i++)
    System.out.println(String.valueOf(bob.marked[i]) + "t" + String.valueOf(bob.edgeTo[i]));

    for (int j = 0;j < Math.abs(bob.marked.length-bob.edgeTo.length); j++)
    if (bob.marked.length < bob.edgeTo.length)
    System.out.println("tt" + String.valueOf(bob.edgeTo[lesserArr + j]));
    else
    System.out.println(String.valueOf(bob.marked[lesserArr + j]));

    }





    share|improve this answer





























      1














      In case your arrays are of different lengths:



      System.out.println("marked edgeTo");
      System.out.println("------- --------");
      int lesserArr = Math.min(bob.marked.length, bob.edgeTo.length);
      for (int i = 0; i < lesserArr;i++)
      System.out.println(String.valueOf(bob.marked[i]) + "t" + String.valueOf(bob.edgeTo[i]));

      for (int j = 0;j < Math.abs(bob.marked.length-bob.edgeTo.length); j++)
      if (bob.marked.length < bob.edgeTo.length)
      System.out.println("tt" + String.valueOf(bob.edgeTo[lesserArr + j]));
      else
      System.out.println(String.valueOf(bob.marked[lesserArr + j]));

      }





      share|improve this answer



























        1












        1








        1







        In case your arrays are of different lengths:



        System.out.println("marked edgeTo");
        System.out.println("------- --------");
        int lesserArr = Math.min(bob.marked.length, bob.edgeTo.length);
        for (int i = 0; i < lesserArr;i++)
        System.out.println(String.valueOf(bob.marked[i]) + "t" + String.valueOf(bob.edgeTo[i]));

        for (int j = 0;j < Math.abs(bob.marked.length-bob.edgeTo.length); j++)
        if (bob.marked.length < bob.edgeTo.length)
        System.out.println("tt" + String.valueOf(bob.edgeTo[lesserArr + j]));
        else
        System.out.println(String.valueOf(bob.marked[lesserArr + j]));

        }





        share|improve this answer















        In case your arrays are of different lengths:



        System.out.println("marked edgeTo");
        System.out.println("------- --------");
        int lesserArr = Math.min(bob.marked.length, bob.edgeTo.length);
        for (int i = 0; i < lesserArr;i++)
        System.out.println(String.valueOf(bob.marked[i]) + "t" + String.valueOf(bob.edgeTo[i]));

        for (int j = 0;j < Math.abs(bob.marked.length-bob.edgeTo.length); j++)
        if (bob.marked.length < bob.edgeTo.length)
        System.out.println("tt" + String.valueOf(bob.edgeTo[lesserArr + j]));
        else
        System.out.println(String.valueOf(bob.marked[lesserArr + j]));

        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 5:43

























        answered Nov 13 '18 at 5:38









        janeonjaneon

        514




        514













            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto