This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 6 years ago.
I know this isn't a difficult issue but I have come to a complete stand still, I want to print a two dimension array with values already assigned for example:
int array1[][] = new int[1][1];
array1[0][0] = 10;
array1[0][1] = 20;
array1[1][0] = 30;
array1[1][1] = 40;
I just want to simply print the values and I really can't remember how to do it, I keep on doing this
System.out.println(Arrays.toString(array1))`;
but I know this is wrong, can you help?
You may want to look at the Arrays.deepToString method, which is similar to Arrays.toString except that if the nested objects are arrays, it will recursively (and correctly) convert them into strings as well.
Also, note that there's a slight typo in your code - you need to size the array as
int[][] array1 = new int[2][2];
since you want a 2x2 array, not a 1x1 array.
Related
This question already has answers here:
Declaring arrays without using the 'new' keyword in Java
(5 answers)
Closed 4 years ago.
I have been programming for quite sometime and I came across something I never noticed before.
What is the difference between these two?
double[] nums = {1,2,3,4};
double[] nums2 = new double[]{1,2,3,4,};
They both compile and have the same properties. At first I thought that nums2 could accept a new int[] and have integer values in it since its lower down in the hierarchy. But It actually didn't work.
The first way is just a shortcut syntax to create and initialize an array of the second way.
This is the only difference.
Take a look at https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
This question already has answers here:
When changing the value of a variable in C, is a new primitive created or is the current primitive mutated?
(2 answers)
Closed 4 years ago.
I have a very basic, silly question.
Let's say in a function foo(), I put:
int a = 3; // after this, I know it will allocate a space in stack and put 3 in it.
Then, I put a = 5; // Here is my question:
What happened after I put a = 5?
Will a new space is allocated and put 5 in it? Or it will find out the new space in stack and then put 5 in it?
No it will not create new space just change value but if you pass this variable to a function will create new space as argument
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 5 years ago.
how to use toString() method in arrays bigger than one dimensional for example
int[][][] array = new array[x][y][z];
print one dimensional
Ststem.out.print(array[a][b].toString());
print all dimensional
System.out.print(array.toString());
my console only show :
[I#15db9742
You have a multidimentional array so you need to do:
Arrays.deepToString(array);
or nest for loops and print the element at index i,j,k
It looks like more like Java :).
In any case, I'd create a function that iterates through the different levels and print each element of the array.
This question already has answers here:
How do I determine whether an array contains a particular value in Java?
(30 answers)
Closed 7 years ago.
I am doing a "Rock,Paper,Scissors" game with Java and I have a question, is there any way to create and Array with answers and check if any of these values equal to another array? For example:
Here is the answerArray (also I want to have short cuts for answers):
String[] answersArray = {"Rock","R","Paper","P","Scissors","S"};
And here is a randomArray for computer:
String[] npcAnswer = {"Rock","Paper","Scissors"};
int idx = new Random().nextInt(npcAnswer.length);
String npcRandomAnswer = (npcAnswer[idx]);
So, I want to check through the scanner if my answer (answersArray) equal to npcRandomAnswer.
Sorry if I have grammar mistakes, I did my best to explain my point.
Thank you.
You can only compare apples with apples. Or actually you can compare apples with oranges, but then you don't really need to compare them, you know the answer will always be false.
It looks like you don't really know yet what will your algorithm be.
I suggest you to concentrate on the algorithm, write pseudo code, and when you have it, try to make java out of it. If you'll have problem with the java, we can easily help you if you post your algorithm (in a new question)
This question already has an answer here:
Arrays with trailing commas inside an array initializer in Java
(1 answer)
Closed 7 years ago.
int [][] a = {{1,2,},{3,4}};
int [][] a = {{1,2},{3,4}};
i have two Multidimensional arrays in java. I want to know what's the exact difference in both of them.just see there is a comma after 2 in first one.
JLS 10.6. Array Initializers: A trailing comma may appear after the last expression in an array initializer and is ignored.
There is no difference. The extra comma might be useful if you are formatting your elements one per line, so that every element appears the same.