I want to break a one-dimensional array in rows.
Array dimension 50. I need to output the array to the console with 10 elements per line. (lang Java 1.8) Thanks!
public void print() {
for (int i = 0; i < arr.length; i++) {
if (i<=9) {
System.out.print(arr[i] + " ");
}else {
System.out.print("\r");
}
}
}
Sample output
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16
etc....
You can see it from 2 differents point of view
Each 10 numbers, print a new line : when the index ends with a 9 you reach ten elements so you print a new line println()
public void print() {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
if (i % 10 == 9) {
System.out.println();
}
}
}
Print enough number of line and on each one : print 10 elements
public void print() {
for (int i = 0; i < arr.length / 10; i++) {
for (int j = 0; j < 10; j++) {
System.out.print(arr[i * 10 + j] + " ");
}
System.out.println();
}
}
Code for any number of elements per line:
public void print(int elementsPerLine) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
if (i % elementsPerLine == 0 && i > 0) {
System.out.println();
} else {
System.out.print(" ");
}
}
}
Use the following code,
public static void printResult(int[][] result)
{
for(int i=0; i<5; i++)
{
for(int j=0; j<10; j++)
{
System.out.print(result[i][j] + ", ");
}
System.out.println();
}
}
public static int[][] modifyArray( int[] singleArray )
{
int columns = 10;
int rows = singleArray.length/10;
int[][] result = new int[rows][columns];
for(int i=0; i<rows; i++)
{
for(int j=0; j<columns; j++)
{
result[i][j] = singleArray[columns*i + j];
}
}
return result;
}
public static void main(String[] args)
{
int[] singleArray = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50};
int[][] result = modifyArray( singleArray);
printResult( result );
}
You must use the modulo operator
https://en.wikipedia.org/wiki/Modulo_operation
public void print() {
for (int i = 0; i < arr.length; i++) {
if (i%10 == 0) {
System.out.print("\r");
}else {
System.out.print(arr[i] + " ");
}
}
}
Maybe you could write something like
if (i % 10 == 0)
System.out.print("\r");
}
System.out.print(arr[i] + " ");
Take slices of 10 items into a new array each time and print that array:
int count = 10;
int iterations = (arr.length / count) + ((arr.length % count) > 0 ? 1 : 0);
for (int i = 1; i <= iterations; i++) {
int[] slice = new int[count];
System.arraycopy(arr, (i - 1) * count, slice, 0, i == iterations ? (array.length % count) : count);
System.out.println(Arrays.toString(slice));
}
The above code works for any value of count.
Take a look at a code down below:
public class PrintEach10thElements {
/**
* #param args the command line arguments
*/
static List<Integer> arrays = new ArrayList<>();
public static void main(String[] args) {
//create 50 elements in an array
for (int i = 0; i <= 49; i++) {
arrays.add(i);
}
for (int g = 0; g < arrays.size(); g++) {
if ( arrays.get(g) % 10 != 0) {
System.out.print(arrays.get(g) + " ");
} else {
System.out.println();
System.out.print(arrays.get(g) + " ");
}
}
}
}
If you don't mind using a Guava library, you can also do this
List<Integer> myList = Arrays.asList(myArray);
System.out.println(Lists.partition(myList, 10).stream()
.map(subList -> subList.stream()
.map( Object::toString )
.collect(Collectors.joining(" ")))
.collect(Collectors.joining("\n")));
I want to print the following pattern in java:
a+1357+1
b+246+2
a+13+3
b+2+4
following is my code, but with this i can only print odd no. or only even no.s
public static void main(String[] args) {
int rows = 7;
for(int i = rows; i >= 1; i=i-2) {
for(int j = 1; j <= i; j=j+2) {
System.out.print(j + " ");
}
System.out.println();
}
}
DEMO
var rows = 4;
for (var i = 4; i > 0; i--) {
for (var j = 1; j <= i; j++) {
document.write((i % 2) + (2 * j) - 1 + " ");
}
document.write('<br>');
}
public static void main(String[] args) {
int rows = 4;
for(int i = rows; i > 0; i--) {
for(int j = 1; j <= i; j++) {
System.out.print((i%2)+(2*j)-1 + " ");
}
System.out.println();
}
}
You need to make a pattern for it. here you can use (i%2)+(2*j)-1
With only a few updates of your code (but not very readable):
int rows = 7;
for (int i = rows; i >= 1; i = i - 2) {
System.out.print((((i + 1) % 4) == 0 ? "a" : "b") + " + ");
for (int j = 1; j <= i; j = j + 2) {
System.out.print((j + ((i + 2) % 4) / 2));
}
System.out.println(" + " + (10 - i) / 2);
}
But instead of using my code, I suggest you write down exactly how the "pattern" is defined and write new code based on your specification. These loops are not optimal.
**This is my code that look a way to find a sum in an array **
public class Piecedemonei {
public static void recherche(int[] tab) {
int num;
int quo;
for (int i = 0; i <= tab.length - 1; i++) {
int somme = 18;
System.out.println("Solution " + i);
for (int j = i; j < tab.length; j++){
if (tab[j] <= somme) {
num = somme / tab[j];
System.out.print(num+"*" + " " + tab[j]);
System.out.println(" ");
somme -= num * tab[j];
j=0;
}
}
}
}
public static void main(String[] args) {
int aba[] = { 7, 6, 4, 5 };
recherche(aba);
System.out.println();
}
}
Output
Solution 0
2 7 + 1 4
Solution 1
Solution 2
Solution 3
**I am looking a way to improve my code so that the output looks like this : **
Solution 0
2*7 + 1*4
Solution 1
3*6
Solution 2
4*4 + 1*4
Solution 3
**Why cant I reset my loop so that it does the same thing again and again ? **
Change
int somme = 18;
for (int i = 0; i <= tab.length - 1; i++) {
System.out.println("Solution " + i);
for (int j = 0; j < tab.length; j++){
...
to
for (int i = 0; i <= tab.length - 1; i++) {
int somme = 18;
System.out.println("Solution " + i);
for (int j = i; j < tab.length; j++) {
...
Notice the int j = i in the inner loop.
I am trying to produce this output without multiplication:
81 100 121 144 169 196 225 256 289
I produced that with this for loop:
for (int k = 9; k <=17; k++){
System.out.print( k * k +" ");
}
But again, I am trying to create a for loop or nested for loops producing the same output without multiplication.
Pseudocode:
Start with value 81
Increment by 19
On the second loop add ++3 to the 19 value
I have not been able to get to the second for loop stage I am confused. Any feedback or push in the right direction is appreciated.
/**
* Created on 8/28/15.
* Following Reges, Stuart, and Martin Stepp. Building Java Programs: A Back to Basics Approach. 3rd Edition.
* Chapter 2 Self-Check Problems & Exercises
*/
public class Ch2_SelfCheckProblems {
public static void main(String[] args) {
ex2();
}
public static void ex2(){
for (int i = 81; i <= 289; i=i+19){
/*for (int j = 1; j >=( i - 81 );j++){
// System.out.print(j+" ");
// System.out.print(i + " ");
}*/
// System.out.print(i + " ");
}
// System.out.println();
/* for (int k = 9; k <=17; k++){
System.out.print( k * k +" ");
}*/
for (int k = 81; k <=289; k++){
for (int j = 1; j <= 1; j++){
k = k + 8;
}
System.out.print( k +" ");
}
}
If I understand your question, you could use addition instead of multiplication. Something like
public static void main(String[] args) {
for (int k = 9; k <= 17; k++) {
int t = 0;
for (int j = 0; j < k; j++) {
t += k;
}
System.out.print(t + " ");
}
System.out.println();
}
Output is (the requested)
81 100 121 144 169 196 225 256 289
Another option would be Math.pow(double, double)
public static void main(String[] args) {
for (int k = 9; k <= 17; k++) {
System.out.print((int) Math.pow(k, 2) + " ");
}
System.out.println();
}
for the same output. Finally, from #mschauer's comments below, you could also use something like
public static void main(String[] args) {
for (int r = 64, k = 9; k < 18; k++) {
r += k + k - 1;
System.out.printf("%d ", r);
}
System.out.println();
}
I have a tutorial in class today with bubble sort and I've got an error I don't know how to fix.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at BubbleSorter.main(BubbleSorter.java:24)
It is not assessed but I would like to get through it to continue on.
Thank you. Below is my entire code.
public class BubbleSorter {
public static void main(String[] args)
{
int i;
int array[] = { 12, 9, 4, 99, 120, 1, 3, 10 };
System.out.println("Array Values before the sort:\n");
for (i = 0; i < array.length; i++)
System.out.print(array[i] + " ");
System.out.println();
System.out.println();
bubble_srt(array, array.length);
System.out.print("Array Values after the sort:\n");
for (i = 0; i < array.length; i++)
;
System.out.print(array[i] + " ");
System.out.println();
System.out.println("PAUSE");
}
private static void bubble_srt(int[] array, int length) {
int i, j, t = 0;
for (i = 0; i < length; i++) {
for (j = 1; j < (length - 1); j++) {
if (array[j - 1] > array[j]) {
t = array[j - 1];
array[j - 1] = array[j];
array[j] = t;
}
}
}
}
}
You have a small mistake:
This:
for (i = 0; i<array.length; i++);
System.out.print(array[i] + " ");
should be:
// v - Semicolon removed
for (i = 0; i<array.length; i++)
System.out.print(array[i] + " ");
Also change
for (j = 1; j < (length - 1); j++) {
to
for (j = 1; j < length; j++) {
You left out the last element of the Array!
Output
Array Values before the sort:
12 9 4 99 120 1 3 10
Array Values after the sort:
1 3 4 9 10 12 99 120
PAUSE
You need to change 2 lines
for (i = 0; i < array.length; i++)
;
System.out.print(array[i] + " ");
to
for (i = 0; i < array.length; i++)
System.out.print(array[i] + " ");
and
for (j = 1; j < (length - 1); j++) {
to
for (j = 1; j < (length); j++) {
On line 18 you need to get rid of the ; or put in brackets {