unable to printout arrayOfInt initialised within the class constructor [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'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);
}
}

Related

How to accept inputs like {1,2,3,45} as an array in a method? [duplicate]

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

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/

How do use the same array of 10000 integers for two different programs?

For my program I have to compare the runtimes of sorting an array using bubble sort. The other uses parallel processing with bubble sort. The same array of 10000 integers(random integers) must be used. My program keeps creating a new array.
I tried creating an array class with an static array and a static variable called k. I have it so that when each program makes an array object, it calls an static instance method to populate the array. The method has a for-loop nested within an if-statement. After first calling the method, the variable k increments. Once the method is called once more, the if statement checks if the method was called before. If yes, the for-loop to repopulate the array is skipped and the program simply return the array with the variables it is already populated with.
public class Array
{
static int i;
static int[] array = new int[10000];
static int k;
Array()
{
populate();
}
public static int[] populate()
{
if(k==0)
{
for(i = 0; i < array.length; i++)
{
array[i] = (int)(Math.random() * 10000);
}
}
return array;
}
}
I am supposed to be getting the same array with every run. PLEASE HELP!

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

Beginner Java methods [duplicate]

This question already has answers here:
Non-static variable cannot be referenced from a static context
(15 answers)
Closed 5 years ago.
public class Test {
int[] a1 = {1, 3, 5, 7, 2};
int[] a2 = new int[a1.length + 10];
public static int[] Array(){
for(int i = 0; i < a1.length; i++){
a2[i] = a1[i];
}
a1 = a2;
}
public static void main(String args[]){
System.out.println(a1.toString);
}
}
I am trying to create a method "Array" that will take in arrays a1 and a2 and add + 10 to the length of a1. My problem is that I don't know how to correctly name the method and how to call it in the main in order to print.
I have tried passing a1 and a2 in the constructor for Array but it doesn't work. I have also tried printing directly in the main() and it doesn't work either. What am I missing?
No idea what are you trying to do but in Java you can't change the size of array once it is declared but if you want to work with array with can change size then you should use arraylist not array and one more thing setting b = awould not work btw here is a code which swaps elements from one array to other but once again you can't change size of array in java: I return b back but you don't need to return an array back because an array variable(name of array) refers to a memory location.
public static int[] extendLength(int [] a, int [] b){
for(int i = 0; i < a.length; i++){
b[i] = a[i];
}
return b;
}
public static void main(String args[]){
int[] a1 = {1, 3, 5, 7, 2};
int[] a2 = new int[a1.length + 10];
int [] res = extendLength(a1,a2);
for(int i = 0; i < a2.length;i++)
System.out.println(a2[i]);
}
Long story short, the way you have your program set up, your variables will also need to be static; otherwise, your static methods will not be able to access them.
Static fields and methods are essentially singular to the class; therefore without a director to which object contains the field, asking a static method to manipulate a non-static field is very ambiguous.
I'm seeing a number of other problems in this program as well; namely, you're treating toString as a field (it's a method), your array method is missing a return statement, and your alignment is all over the place. This probably isn't an appropriate question to bring to Stack Overflow; I would encourage you to look at a tutorial, like the one at Tutorials Point, before bringing your question here.

Categories