how to print the elements of an array [duplicate] - java

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 7 years ago.
I am trying to print the elements of this array, but it is printing junk characters. What is the problem in this program?
class Demo{
public static void main(String[] args){
int[] x= new int []{5,6,7,8,9,10,11};
{
System.out.println(x);
}
}
}

You need to use the Arrays.toString() method:
System.out.println(Arrays.toString(x));
This gives you a meaningful representation of the data inside your array. The default toString of Java array objects is not a meaningful representation of the data.

public static void main(String args[]) {
int[] x= new int[]{5,6,7,8,9,10,11};
{
System.out.println(Arrays.toString(x));
}
}

Related

Cant arrays be changed by a method? [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 2 months ago.
public class lalak
{
public static void m1(int[] array)
{
array= new int[]{1,2,3,4,5};
System.out.println(array[2]);
}
public static void main(String[] args)
{
int[] array = {1,1,1,1,1};
m1(array);
System.out.println(array[2]);
}
}
why answer is 1, not 3?
i expected the program to print 3 but i got 1 as output.
i thought method would change my original array but it turned out to be false. does anyone know the reason for this?
You're creating a new array which makes the reference to your array in main redundant.
You could either, replace the first line like Jon Skeet said
Or another option would be, to have m1 return an array and assign it to your array in main
public class lalak
{
public static int[] m1(int[] array)
{
array= new int[]{1,2,3,4,5};
System.out.println(array[2]);
}
public static void main(String[] args)
{
int[] array = {1,1,1,1,1};
array = m1(array);
System.out.println(array[2]);
}
}

Error calling my array method in Java (easy question) [duplicate]

This question already has answers here:
Java arrays printing out weird numbers and text [duplicate]
(10 answers)
Closed 1 year ago.
I'm sure this is dumb, I'm just learning to code and trying to follow instructions on how to make this method that multiplies doubles into an array. The code compiles but does not return my array, it just gives me [D#76ed5528 which I'm assuming is the memory address of the array?
public class Ex1 {
public static void main(String[] args) {
Ex1 exone = new Ex1();
double[] bob = exone.square(2, 6, 9, 8);
System.out.println(bob);
}
public double[] square(int a, int b, int c, int d) {
double[] result = {a*a, b*b, c*c, d*d};
return result;
}
}
Yes #Drunkasaurus, the [D#76ed5528 you mentioned in is indeed the hash for the array in memory. In order to print the values in your array you have a few options including:
Using the Arrays.toString() method: https://stackoverflow.com/a/29140403/10152919
Using a simple loop:
https://stackoverflow.com/a/409795/10152919
Also, no question is a dumb question if you can learn from it 🙂
This is because you are directly trying to return the result array.
Try to iterate over the array.
For ex - https://www.geeksforgeeks.org/iterating-arrays-java/

Passing an array in Java [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 5 years ago.
Why is the output for the following code 11,21,31 11,21,31 and not 10,20,30 10,20,30?
public class Tp {
public static void doChange(int a[])
{
for(int pos=0;pos<a.length;pos++)
{
a[pos]+=1;
}
}
public static void main(String args[])
{
int arr[]= {10,20,30};
doChange(arr);
for(int x:arr)
{
System.out.print(x+",");
}
System.out.println(arr[0]+" "+arr[1]+" "+arr[2]);
}
}
When you do
public static void doChange(int a[])
{
for(int pos=0;pos<a.length;pos++)
{
a[pos]+=1;
}
}
This line a[pos]+=1;, adds +1 to each value on the array, so the expect output is 11,21,31 11,21,31, if you want 10,20,30 10,20,30, delete or comment this line doChange(arr);
Because in java arrays are objects, when you pass the arr array to the doChange() method as argument, it actually copies the reference id of that array (not the elements).
In a simple way, you are updating the same array that you passed in the doChange() method, that's why it reflects to the original array.
Obviously it prints "11, 21, 31, 11,21,31", with the method doChange you add 1 to every element of the array.
Writing:
a[pos]+=1;
is the same as writing:
a[pos]=a[pos]+1;
int arr[]= {10,20,30}; Initializes array [0]=10, [1]=20, [2]=30
doChange called with a[] which is a reference of arr[]
for-loop adds +1 to every value in arr[] ( += is equal to x = x + ? )
Now you have [0]=11, [1]=21, [2]=31

unable to printout arrayOfInt initialised within the class constructor [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 7 years ago.
I'm new to Java and is trying to learn the concept of constructor. I tried to print out the value of arrayOfInts in the main method using (to test whether the constructor was initialised the way I expected)
System.out.println(ds.arrayOfInts);
However, instead of printing out the values, the output is:
[I#15db9742
Why am I getting the wrong result and how I can print out the correct result? (i.e. the value stored in arrayOfInts).
public class DataStructure {
public static void main(String[] args) {
DataStructure ds = new DataStructure();
//System.out.println(ds.arrayOfInts); Doesnt work as expected
}
private final static int SIZE = 15;
private int[] arrayOfInts = new int[SIZE];
public DataStructure() {
int arrayValue = 0;
for (int i = 0; i < SIZE; i++) {
arrayOfInts[i] = ++arrayValue;
}
}
}
You are trying to print an array. An array is an object.
In order to display it correctly, you can loop through it, or use the Arrays.toString() method:
System.out.println(Arrays.toString(ds.arrayOfInts));
which returns a string representation of the specified array.
Arrays are objects in java. You need to iterate over elements and print them. Here is a sample implementation using the for-each loop.
public void print() {
for (int x : arrayOfInts) {
System.out.println(x);
}
}

Java string input and output string [duplicate]

This question already has answers here:
Java arrays printing out weird numbers and text [duplicate]
(10 answers)
Closed 8 years ago.
and thanks for reading this question from a beginner.
I tried to run this code on the command:
public class a{
public static void main(String[] args){
String[] s = args;
System.out.println(s);
}
}
and the following input: "java a this is string"
gives me:
"[Ljava.lang.String;#65d4ab0e" as an output.
What should I do to get "this is string" as output?
You are printing the array object itself, not the content of the array.
The hashcode value [Ljava.lang.String;#65d4ab0e indicates that, s is an array and it's has String values. The leading [ says, the object you have printed is an array.
public class a{
public static void main(String[] args){
String[] s = args;
System.out.println(Arrays.toString(s));
}
}
Use Arrays.toString(s)
Take a look at http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString(java.lang.Object[])

Categories