Java: Print Elements of Array and Insert Line Break

Java: Print elements of array and insert line break

this will work for you

for (int i = 0; i< n.length; i++) {
System.out.print(n[i]);
// every 68 iterations prints a \ and new line
if((i + 1) % 68 == 0) System.out.println("\\");
}

add a new element to an array as a line break

Use double quotes "\n" instead of '\n' single quotes, Single quotes treats as string

<?php
$arr = [1,2,3,"\n","after line break"];
echo implode('',$arr);
?>

Live demo : https://eval.in/865569

In your browser to add line break use <br> instead \n

How to print each element of an array to a different line?

To print element on a new line just simply add another for-loop.

for (int i = 0; i < nums.length; i++) {
System.out.println(nums[i]);
}

OR
try this..

for (int i = 2; i < nums.length; i++) {
nums [i] = nums [i - 1] + nums [i - 2];
System.out.println(nums[i]);
}

printing an array in multiple lines

Try this,

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static int[] array;

public static void main(String[] args)
{
array = new int[25];
for(int i=0; i<25; i++)
array[i] = i + 1;
printArray();
}

public static void printArray()

{
int i;
for(i=1; i<=25; i++){
if (i % 5 != 0)
System.out.print(array[i-1]+",");
else
System.out.println(array[i-1]);
}
}
}

Javascript how to show each element of array on a new line

The for-loop is suspicious. Firstly, you do not process all items (the last one is missing, as @sarfraz pointed out). Sencondly you are returning the result (zzz) in the for-loop body:

for (var i=0; i<=xxx; i++)
{
zzz[i] = zzz[i] + '<br />';
return zzz; // for-loop will stop here! resulting in ["value1<br />", "Value2", etc...]
}

In Javscript you can simple "join" the array:

return dates.split(',').join("<br />")

Since you are simply replacing strings you could use the replace method:

return dates.replace(",", "<br />");

Display each list element in a separate line (console)

Iterate through the elements, printing each one individually.

for (String element : list) {
System.out.println(element);
}

Alternatively, Java 8 syntax offers a nice shorthand to do the same thing with a method reference

list.forEach(System.out::println);

or a lambda

list.forEach(t -> System.out.println(t));

How to print some elements of an array in a new line in Ruby?

Formatting an array can be done with the [%] method1

arr.each_slice(3){|trio| puts "%s: %d, %d" % trio}

Print in new line, java

    String newLine = System.getProperty("line.separator");//This will retrieve line separator dependent on OS.

System.out.println("line 1" + newLine + "line2");

How to add newline when I return array in React?

Wrap the string in a <div> and then push it into array.

dummyDataLoop = () => {
var rows = [];
for (let i = 0; i < 10; i++) {
rows.push(<div>dummy data</div>);
}
return rows;
};

Another way is to use map() and spread operator to shorten the code.

dummyDataLoop = () => [...Array(10)].map(x => <div>dummy data</div>)


Related Topics



Leave a reply



Submit