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/
Related
This question already has answers here:
In Java, when is the {a,b,c,...} array shorthand inappropriate, and why?
(4 answers)
Closed 1 year ago.
I can only edit the class Solution and the method inside it.
The input will be given by the problem only.
further details: I basically have to create a method in a class which accepts two arrays or whatever that input is trying to say and pick out one element which is extra in one of them.
my Code:
import java.util.Arrays;
public class Solution {
public static int solution(int[] x, int[] y) {
// Your code here
int[] shorter = (x.length>y.length)? y : x;
int[] longer = (x.length>y.length)? x : y;
Arrays.sort(shorter);
Arrays.sort(longer);
for(int i=0; i<shorter.length; i++){
if(shorter[i]!=longer[i]){
//System.out.println(longer[i]);
return longer[i];
}
else {
// System.out.println(longer[longer.length-1]);
return longer[longer.length-1];
}
}
return 0;
}
}
The input that they are giving refers to the arrays which are missing new int[] ...
It is possible but you need to accept input as a string and need to parse it afterward to get desired integers
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);
}
}
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));
}
}
This question already has answers here:
Difference between int[] array and int array[]
(26 answers)
Closed 10 years ago.
When I want to implement a function in C++ , if it matters to receive the int array in the following cases?
void fn1(int []a) {
a[0] = 1;
}
void fn2(int a[]) {
a[0] = 1;
}
In Java, there is no semantic difference.
In C++, the first syntax is invalid.
In Java,the declaration is same...
but in C++,the fn1() declaration need to be different
Well, the question is not clear.. Whether to receive the int array or not, it depends on the logics of your method. In Java it is better to write a[], but you can write either way.
Also, look over here - pass array to method Java
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to write a basic swap function in Java
Hi. I don't know java at all, and in the near future have no wish to study it. However I have lots of friends that are java programmers and from conversations I learnt that there is no analog of C#'s ref keyword in Java. Which made me wonder how can one write a function that swaps two integers in Java. My friends (though not very good java experts) could not write such a function. Is it officially impossible? Please note, that I understand that one can swap two integers without a function, the question is exactly to write a function that takes two integers and swaps them. Thanks in advance.
Short: You can't.
Long: You need some workaround, like wrapping them in a mutable datatype, e.g. array, e.g.:
public static void swap(int[] a, int[] b) {
int t = a[0]; a[0] = b[0]; b[0] = t;
}
but that doesn't have the same semantic, since what you're swapping is actually the array members, not the a and b themselves.
In Java, all the arguments are passed by value. So there is no such thing as a ref.
However, you might achieve variable swapping by wapping values in objects (or arrays).
public class Holder<T> {
public T value = null;
public Holder(T v) { this.value = v; }
}
public static <T> void swap(Holder<T> a, Holder<T> b) {
T temp = a.value; a.value = b.value; b.value = temp;
}