How to represent static struct in Java
Following is a static struct in C++. How can this be represented i开发者_StackOverflown java.
static struct {
int c1;
int c2;
} pair[37]= {{3770,3780}, {3770,3781}, {3770,3782}, {3770,3785},
{3770,3786}, {3770,3787}, {3771,3780}, {3771,3781},
{3771,3782}, {3771,3785}, {3771,3786}, {3771,3787},
{3772,3780}, {3772,3783}, {3773,3780}, {3773,3781},
{3773,3782}, {3773,3785}, {3773,3786}, {3773,3787},
{3774,3780}, {3774,3781}, {3774,3782}, {3774,3783},
{3774,3785}, {3774,3786}, {3774,3787}, {3776,3780},
{3776,3785}, {3776,3786}, {3776,3787}, {53,3770},
{53,3771},{53,3772},{53,3773},{53,3774},{53,3776}};
Thanks
In java you could make a collection/array of Pair objects or use an multidimensional array (array of arrays);
static int[][] pairs = new int[][] { {3770,3780}, {3770,3781}, {3770,3782}, {3770,3785} }
or
class Pair {
int a;
int b;
Pair(int a, int b) { this.a=a; this.b=b; }
}
static Pair[] pairs = new Pair[] { new Pair(1,2), new Pair(2,3) ..... }
There is no "static struct". What you have is equivalent to:
struct PairType {
int c1;
int c2;
};
static PairType pair[37]= {
{3770,3780}, {3770,3781}, {3770,3782}, {3770,3785},
{3770,3786}, {3770,3787}, {3771,3780}, {3771,3781},
{3771,3782}, {3771,3785}, {3771,3786}, {3771,3787},
{3772,3780}, {3772,3783}, {3773,3780}, {3773,3781},
{3773,3782}, {3773,3785}, {3773,3786}, {3773,3787},
{3774,3780}, {3774,3781}, {3774,3782}, {3774,3783},
{3774,3785}, {3774,3786}, {3774,3787}, {3776,3780},
{3776,3785}, {3776,3786}, {3776,3787}, {53,3770},
{53,3771},{53,3772},{53,3773},{53,3774},{53,3776}
};
and the C++ grammar allows the type definition to replace the type name in a variable declaration.
Probably you know how to convert these two independent parts to Java?
This is probably the best you can do in (idiomatic) Java:
final class Pair<A, B> {
public final A first;
public final B second;
private Pair(A first, B second) {
this.first = first;
this.second = second;
}
public static <A, B> Pair<A, B> of(A first, B second) {
return new Pair<A, B>(first, second);
}
}
List<List<Pair<Integer, Integer>>> pairs = Arrays.asList(
Arrays.asList(Pair.of(3234, 3235), Pair.of(5678, 5679)),
Arrays.asList(Pair.of(3456, 3457), Pair.of(2367, 2368))
);
In Java there are no structs. You use classes in a similar way.
In this case, you should have a class for the data structure you intend to keep.
One of the many possible implementations would be:
public class DataStructure {
private int c1;
private int c2;
public DataStructure(int c1, int c2) {
this.c1 = c1;
this.c2 = c2;
}
public int getC1() {
return c1;
}
public void setC1(int newC1) {
c1=newC1;
}
... //Same for C2
}
}
Then you can use a single array of pairs as a static variable for a specific class, and you can have your class define a static array of those DataStructure objects, composed of 2 integers each like yo udefined, or composed of anything you want, if you define the class in a different way.
There is no direct translation in Java.
In place of a struct, an inner class can be used, assuming you plan to modify the fields of each array element.
To closely model the c/c++ semantics, you can make the member variables public
scoped.
If you intend for these to be read-only, you can make them final
. Or you can even go as far as defining an enum for this dataset, if the number of elements are also fixed.
There isn't a very good way to reduce the 'ceremony' of writing a bunch of new Pair(...)
. When all the fields are of the same type, you can write a factory method that takes a n-elements x n-fields array of the parameters... but you lose some compile-time correctness checking.
精彩评论