How to print [ ] in an array (Java)

how to print array in java using for loop
how to print array in java without loop
java print 2d array
how to print an array c++
how to print string array in java using for loop
java print arraylist
how to print an array in c
java arrays

I want to take 10 numbers from the user and input them into a new array. How can I put [ ] into my output?

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;

public class Main {
public static void main(String[] args) {
    Scanner scnr = new Scanner(System.in);
    int num = 10;
    List<Integer> inputList = new ArrayList<>(num);
    while (num-- > 0) {
        inputList.add(scnr.nextInt());
    }
    List<Integer> goofyArray = new ArrayList<>(inputList.size());
    for (int i = inputList.size() - 1; i >= 0; i--) {
        if(inputList.get(i) % 7 == 0){continue;}
        if(inputList.get(i) < 0){
            goofyArray.add((inputList.get(i) * -1) * 2);
        } else {
            goofyArray.add(inputList.get(i));
        }
    }
    for (int number : goofyArray) {
        System.out.print( number + " ");
    }
  }
}
Only using static arrays and no extra imports besides Scanner
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        int NUM_OF_INPUTS = 10;
        int [ ] inputArray = new int [NUM_OF_INPUTS]; 
       for (int index = 0; index < NUM_OF_INPUTS; index++){
        inputArray[index] = scnr.nextInt(); 
       }

        int[] goofyArray = new int[inputArray.length];
        int[] arrayWithoutSevensAndReversed; // make a new array because static arrays don't have a remove
        int multiplesOfSeven = 0;

        // for loop 1: reverse and check how many multiples of 7
        for (int i = 0; i < goofyArray.length; i++) {
            goofyArray[i] = inputArray[goofyArray.length - 1 - i]; // Reverse the array.
            if (goofyArray[i] % 7 == 0)
                multiplesOfSeven++;
        }
        // for loop 2: use the new array with that will not contains multiples of seven the array
        arrayWithoutSevensAndReversed = new int[goofyArray.length - multiplesOfSeven];
        for (int x = 0, y = 0; x < goofyArray.length; x++) {
            if (goofyArray[x] % 7 != 0) { // is not multiple of 7, then add it to the array! 
                arrayWithoutSevensAndReversed[y] = goofyArray[x];
                if (arrayWithoutSevensAndReversed[y] < 0) { // make it positive then double! 
                    arrayWithoutSevensAndReversed[y] = arrayWithoutSevensAndReversed[y] * -1 * 2;
                }
                y++;
            }
        }

        for (int number : arrayWithoutSevensAndReversed) {
            System.out.print(number + " ");
        }
    }
}

Java Program to Print an Array, Since Java 5 you can use Arrays.toString(arr) or Arrays.deepToString(arr) for arrays within arrays. Note that the Object[] version calls .toString() on each object in  You can access the elements of an array using name and position as − System.out.println(myArray[3]); //Which is 1457 Creating an array in Java. In Java, arrays are treated as referenced types you can create an array using the new keyword similar to objects and populate it using the indices as −

How can I put [ ] into my output?

Use Arrays.toString(int array[]) from util package.

System.out.println(Arrays.toString(goofyArray));

What's the simplest way to print a Java array?, are allowed to directly prints its values whenever used inside System. out. println() or format and printf method, Similar to various classes in Java do this by overriding toString() method. Java Program to Print an Array In this program, you'll learn different techniques to print the elements of a given array in Java.

Not sure about what do you mean with put [ ] into my output.Try this:

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        int num = 10;
        List<Integer> inputList = new ArrayList<>(num);
        while (num-- > 0) {
            inputList.add(scnr.nextInt());
        }
        List<Integer> goofyArray = new ArrayList<>(inputList.size());
        //Reverse the array.
        for (int i = inputList.size() - 1; i >= 0; i--) {
            if(inputList.get(i) % 7 == 0){continue;} //doesn't add numbers that are multiples of 7.
            if(inputList.get(i) < 0){
                goofyArray.add((inputList.get(i) * -1) * 2); // Change any numbers that are negative to be positive and twice their value.
            } else {
                goofyArray.add(inputList.get(i));
            }
        }
        int[] output = new int[goofyArray.size()];
        output = goofyArray.toArray(output);
        for (int number : output) {
            System.out.print( number + " ");
        }
    }
}

3 Examples to Print Array Elements/Values in Java , How do I print an array of elements in one line? In this article, we will show you a few ways to print a Java Array. 1. Arrays.toString. This is the simplest way to print an Array – Arrays.toString (since JDK 1.5)

As far as I understand you want to print the array as output.

All you have to do is instead of this:

for (int number : goofyArray) {
    System.out.print( number + " ");
}

do this:

System.out.print(goofyArray.toString());

How to print the results of an array in a single line?, separator at the end of what you just printed. Learn 4 Techniques to PRINT ArrayList Elements in Java with Code Example. To print elements, first we’ll create a String ArrayList and store weekdays name as strings into it and display them using following ways: For-loop; For-each loop; Using iterator; Using List-iterator; Here is a string ArrayList.

To remove elements from array, you can use ArrayUtils.remove(int array[], int index) from org.apache.commons.lang package.

int num[]=new int[] {10,20,30,40,50};
num = ArrayUtils.remove(num, 2); 

Print Two Dimensional Array in Java, package com.mkyong.utils.print; import java.util.Arrays; public class PrintArray { public static void main(String[] args) { // array String[] arrayStr  Java Program to Print Array Elements using For Loop. This program in Java allows the user to enter the Size and elements of an Array.Next, we are using For Loop to iterate each element in this array, and print those array elements.

Java - How to print an Array, Given an array arr in Java, the task is to print the contents of this array. Loop method: The first thing that comes to mind is to write a for loop from i = 0 to n, and​  Ok,Successfully printed elements of one dimension array..Now I am going to consider two dimension array..I'll declare two dimension array as "number2" and print the elements using "Arrays.deepToString()" keyword.Before using that You will have to import 'java.util.Arrays' library.

How to print an Array in Java without using Loop, So, we can store a fixed set of elements in an array. There are following ways to print an array in Java: Java for loop; Java for-each loop; Java Arrays.toString()  In this post we will try to print an array or matrix of numbers at console in same manner as we generally write on paper. For this the logic is to access each element of array one by one and make them print separated by a space and when row get to emd in matrix then we will also change the row.

How to print array in Java, There are multiple ways you can print arrays in Java and the examples given below will walk you through the process. Assume the name of the array to be  Program to print the elements of an array in reverse order. In this program, we need to print the elements of the array in reverse order that is; the last element should be displayed first, followed by second last element and so on.

Comments
  • You cannot remove elements from an array. A solution would be to build up a new array. Or - even better - use a List.
  • For handling negative numbers - format the code - it looks like if (goofyArray[i] < 0) is nested within if ( goofyArray[i] % 7 == 0).
  • Am I missing something? Why nobody is answering the question: "How can I put [ ] into my output?" Do you want to have something like "array[1] = 100"? Or "[100 200 300]"? If so, then you have to build the string either by concatenation (like "[" + i + "] = " + array[i]) or formatting String.format(...)
  • see my updated answer :)
  • what do you mean with put [ ] into my output?.
  • Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at Main.main(Main.java:27)