Declaring an Array and altering its contents [duplicate] - java

This question already has answers here:
Passing directly an array initializer to a method parameter doesn't work
(3 answers)
Closed 8 years ago.
I am writing test code to explore properties of an Array.
Why does this work
public static void main(String[] args){
int[] testing={1,2,3};
for(int i = 0;i<testing.length;i++){
System.out.println(testing[i]);
}
}
and why doesn't this work?
public static void main(String[] args){
int[] testing= new int[3];
testing = {1,2,3};
for(int i = 0;i<testing.length;i++){
System.out.println(testing[i]);
}
}
What is it about Array that prevents this from being valid?

Because the Java Language Specification says so
An array initializer may be specified in a declaration (§8.3, §9.3,
§14.4), or as part of an array creation expression (§15.10), to create
an array and provide some initial values.
So you can either use it as you are already doing
int[] testing = {1,2,3};
or as part of an array creation expression
testing = new int[]{1,2,3};

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/

Creating instance of an object with String[] as one of the parameters [duplicate]

This question already has answers here:
How do I declare and initialize an array in Java?
(31 answers)
Closed 4 years ago.
I was wondering how to create an instance of an object that takes a String[] type as a parameter. So far it comes up with an error saying that I cannot initialise an array when creating a new instance. This is my code:
public class Profile{
public Profile(String[] interests) {
this.interests = interests;
}
}
public class ProfileTest{
public static void main(String[] args) {
//Where the error appears
Profile newProfile = new Profile({"Interest1","Interest2","Interest3"});
}
}
I don't want to use an arraylist or anything - just String[].
To fix the compilation error, use a variable to hold the string array like this:
String[] array = {"Interest1","Interest2","Interest3"};
Profile newProfile = new Profile(array);

Local variable array of ints defaults to zero [duplicate]

This question already has answers here:
int array initialization
(7 answers)
Closed 5 years ago.
Why is it that when an array of 'ints' is made as a local variable they default to zero?
public static void main(String [] args) {
int []arrayInts = new int[5];
for(int i: arrayInts)
System.out.println(i);
//Prints out zeros
}
While if an 'int' variable is declared as a local variable it does not get initialized.
public static void main(String [] args) {
int a;
System.out.println(a);
//Compilation error: The local variable a may not have been initialized
}
These two examples aren't comparable.
In the first one, you are actively initializing your array. The default value for an array of ints is 0.
In the second example, you've not initialized the variable a, either explicitly or implicitly, and you're attempting to make use of it. Java will complain about this action.

how to print the elements of an array [duplicate]

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));
}
}

Categories