How do I call an Array constructor initializing the Array length? - java

How do I make a constructor to set the length of a global array?
I have already tried several ways to do it, none successful.
Example:
public Class{
public Class(int length){
double[] array = new double[length]; <- this is not global
L = length;
}
int L;
double[] array = new double[L]; <- this does not work
}
I need an array with a length determined by Constructor.

I think it's as simple as this:
public class MyClass{
double[] array;
public MyClass(int length){
array = new double[length];
}
}
I've also made the code actually compile :) You were missing some keywords etc.
If you want to access length in your code, use array.length rather than storing it redundantly in a separate field.
Also calling your class Class is a bad choice, even as an example, because it clashes with java.lang.Class.

Declare the array as member variable. Then initialize it in the constructor.
public class A{
private double[] array;
public Class(int length){
array = new double[length];
L = length;
}
}
You could initialize it in second way. But then you need to use a fixed length
public class A{
private double[] array = new double[100]; // use fixed length
public Class(int length){
array = new double[length];
L = length;
}
}

I don't know what you are trying to achieve but why you don't simply do it this way:
public class Class{
public Class(int length){
this.array = new double[length]; // <- this is not global
}
double[] array;
}

public class aClass{
//define the variable name here, but wait to initialize it in the constructor
public double[] array;
public aClass(int length){
array = new double[length];
}
}

You can do it
public class Test {
double[] array;
public Test (int length){
array = new double[length]; <- this is not global
}

Related

How to clone array in java with this code

This code generates unsorted array. My question is how do I clone the generated array so I can reuse the unsorted array in different sorting algorithms.
public class UnsortedData{
private int [] coreData;
private int maxArraySize;
private int currentArraySize;
public UnsortedData(int size){
this.maxArraySize = size;
this.coreData = new int[this.maxArraySize];
this.currentArraySize = 0;
}
public boolean addData(int data){
if (currentArraySize < maxArraySize)
{
coreData[currentArraySize] = data;
currentArraySize++;
return true;
}
return false;
}
public class dataSorting {
public static void main(String[] args) {
Random rand = new Random();
UnsortedData uD = new UnsortedData(1000000);
for (int x = 0; x < 50; x++) {
uD.addData(rand.nextInt(3000000));
}
}
Use build in methods from java.lang.Object to clone the original array and java.util.Arrays to sort the cloned array.
int[] arr = {6,9,4,5}; // original array
int[] arrCopy = arr.clone() // we created a copy of the array
Arrays.sort(arrCopy); // it sort the array that we cloned
References: Arrays Class , Class Object
Easiest way? Object.clone()
int[] arr = coreData.clone();
You can also use the Stream API or external libraries such as Apache Commons.

Calling specific index of Array in setter method

I have a class Gs with the following defined:
private static double[] r = new double[3];
I also create the getter/setter for this array:
public static double[] getR() {
return r;
}
public static void setR(double[] r) {
Gs.r = r;
}
In another class I want to set the value in the array, e.g. just r[1] but I don't know how I can do it with these functions.
How can I set the index of which I wish to set the value to?
r[0], r[1], r[2]....
The setter method expects an array arg double[] rather than double but I don't know how I'd specify it.
You could pass the index as an argument to the getter and the setter:
public static double getR(int index) {
return r[index];
}
public static void setR(int index, double r) {
Gs.r[index] = r;
}
try this
double[] val = new double[3];
//example values
val[0] = 3.4;
val[1] = 24d;
val[2] = 28.4;
Gs.setR(val);
Note: This will set the full array at once
You just can set the the value on the array returned by the getter:
Gs.getR()[1] = .1;

Defining array as class variable with user input for dimensions

I wish to declare an array as a class variable but with dimensions input from the user.
class whatever
{
int array[][]=new int [n][n]; //this is a wrong definition
public void method()
{
//method content
}
}
Here n is the required input.
You can pass n as an argument to the constructor of your class1. Something like
class Whatever {
int[][] array;
public Whatever(int n) {
this.array = new int[n][n];
}
public void method()
{
//method content
}
}
1 And, by convention, Java class names should start with a capital letter.
You can pass array size trough constructor as a parameter and do like this.
class YourClass {
int[][] array;
public YourClass(int size){
array=new int[size][size];
}
public YourClass(int coloumnSize,int rowSize){
array=new int[rowSize][coloumnSize];
}
}
Hope it will help you.

Java - Change values of an array by a method in a Class

Here is my code:
class Myclass {
private static int[] array;
public static void main(String[] args) {
Myclass m = new Myclass();
for (int i = 0; i < 10; i++) {
m.array[i] = i;
System.out.println(m.array[i]);
}
}
public Myclass() {
int[] array = new int[10];
}
}
It throws a java.lang.nullPointerException when trying to do this:
m.array[i] = i;
Can anybody help me please?
You have declared a local variable array in your constructor, so you're not actually initializing the array declared in Myclass.
You'll want to refer directly to array in the constructor. Instead of
int[] array = new int[10];
Use this
array = new int[10];
Additionally, you've declared array static in the scope of your Myclass class.
private static int[] array;
You only have one instance of Myclass here, so it doesn't matter, but normally this would not be static, if you're initializing it in a constructor. You should remove static:
private int[] array;
In your constructor you are making your assignment to a local variable names array, not the static class variable also named array. This is a scope problem.
I'm also guessing that since you access array via m.array, you want a member variable and not a static one. Here's the fix
class Myclass {
private int[] array;
public static void main(String[] args) {
Myclass m = new Myclass();
for (int i = 0; i < 10; i++) {
m.array[i] = i;
System.out.println(m.array[i]);
}
}
public Myclass() {
rray = new int[10];
}
}
in MyClass() type this
this.array = new int [10];
instead of this
int[] array = new int[10];
Your code should be as below. In the constructor, instead of initializing the instance variable you created a new local variable and the instance variable was left uninitalized which caused the NullPointerException. Also the instance variable shouldn't be static.
class Myclass {
private int[] array;
public static void main(String[] args) {
Myclass m = new Myclass();
for (int i = 0; i < 10; i++) {
m.array[i] = i;
System.out.println(m.array[i]);
}
}
public Myclass() {
array = new int[10];
}
}
First, if you plan to use array as a field of m (i.e. m.array) don't declare it as static, but:
private int[] array;
Next thing you have to do is to initialize it. Best place to do that is in the constructor:
public MyClass() {
array= new int[10]; //just array = new int[10]; don't put int[] in front of the array, because the variable already exists as a field.
}
The rest of the code should work.

Advice with sending a reference to another method in the same class

I am working on a program for class and I am stuck. When I try to compile the following code I receive and variable not found error. please help!
public class RingBuffer
{
public RingBuffer(int capacity){
double[] EmptyBuffer = new double[capacity];
System.out.print(EmptyBuffer.length);
}
public int size(){
int size = EmptyBuffer.length;
return size;
}
please note that I am getting an error with the size() method not being able to find the variable EmptyBuffer.
You should probably make that a field, aka an instance variable:
public class RingBuffer
{
private double[] emptyBuffer;
public RingBuffer(int capacity){
emptyBuffer = new double[capacity];
System.out.print(EmptyBuffer.length);
}
public int size(){
int size = emptyBuffer.length;
return size;
}
}
This will make emptyBuffer available throughout your class, i.e. in any other method.
Pass the array as an argument to your size() method like you've done with capacity:
int size(double[] EmptyBuffer){
int size = EmptyBuffer.length;
return size;
}
Then:
double[] EmptyBuffer = new Double[capacity];
int size = size(EmptyBuffer); // make call to size passing the array
// as an argument
System.out.print(size);

Categories