Storing an array of three values ​​in Java

I have a set of three numbers, and I want to save several copies of this set in an array. for example, if I were doing this in C, it would be something like:

struct tParams { int v; double A; double n; }; static struct tParams Params[] = { { 4230, 1.477404177730177e-04, 1.9565}, { 3680, 1.920339268755614e-04, 1.925 }, { 3450, 2.894751026819746e-04, 1.875 }, { 3295, 4.349905111115636e-04, 1.825 }, //... }; 

How can I do this in Java?

thanks

EDIT To clarify, these values ​​will be hard-coded and will be specified only by reference. If I use ListArray classes, it seems like I need to add every object from the method.

I am creating a class with methods that will prepare mathematical operations. Some of these methods use data that I hardcode to obtain coefficients. In C, structures are defined in the main mathOperations class, but not in the method.

+4
source share
4 answers

I think this is the most literal translation of your C code:

  class tParam { int v; double A; double n; private tParam(int v, double A, double n) { this.v = v; this.A = A; this.n = n; } } tParam[] params = { new tParam(4230, 1.477404177730177e-04, 1.9565), new tParam(3680, 1.920339268755614e-04, 1.925), new tParam(3450, 2.894751026819746e-04, 1.875), new tParam(3295, 4.349905111115636e-04, 1.825) }; 

The advantage is that you do not use all those setters that are redundant for your case, as they are "installed once", as you say. In addition, given that the data is a static list, it makes no sense to use the List class - the array is in order.

+2
source

Depending on what you are trying to do.

In general, you probably want to use a class in Java, where you would use a struct in c. Something like this would be the most standard way to do this:

 public class Params { private Integer v; private Double a; private Double n; public Params(int V, double A, double N){ this.v=V; this.a=A; this.n=N; } public Integer getV() { return v; } public void setV(Integer v) { this.v = v; } public Double getA() { return a; } public void setA(Double a) { this.a = a; } public Double getN() { return n; } public void setN(Double n) { this.n = n; } } 

Short variable names are generally considered bad practice in Javaland for class scope variables. Although this could be done without getters and setters, simply by making class variables publicly available, which in general (though not at all universal) is also considered bad practice. For a simple class like this without any behavior, I would just make everything publicly available.

+3
source

One option is to use a class. I don’t think you have getters and setters if you use it for something simple.

 public class TParams { public int v; public double A; public double n; } 

If performance is important, I would consider using parallel arrays of primitives. This will be faster, because you will avoid the overhead associated with the creation of the object. This is especially true if you often change values ​​inside and outside arrays. Google recommends considering this technique if you are programming on Android. You can wrap it all in a class, looking something like this, depending on your needs:

 public class TParamsList { double[] a double[] n; int[] v; public TParamsList(int size) { a = new double[size]; n = new double[size]; v = new int[size]; } public void setValue(int slot, int v, double a, double n) { a[slot] = a; n[slot] = n; v[slot] = a; } public int getV(int slot) { return v[slot]; } public int getA(int slot) { return a[slot]; } public int getN(int slot) { return n[slot]; } } 
+1
source

Instead of your structure, you can have a class:

 public class TParams { private int v; private double A; private double n; public TParams(int v, double A, double n) { this.v = v; this.A = A; this.n = n; } // Getters + Setters } 

Then you can have a list of instances of your class, for example:

 ArrayList<TParams> tParamsList = new ArrayList<TParams>(); tParamsList.add(new TParams(4230, 1.477404177730177e-04, 1.9565)); etc. 

EDIT

You should be able to do this as follows:

 TParams tParamsArray[] = { tParams_1, tParams_2, tParams_3 }; //Where tParams_xx are all instances of your TParams class tParamsList.addAll(Arrays.asList(tParamsArray)); 
+1
source

Source: https://habr.com/ru/post/1340725/


All Articles