How to represent static structure in Java

Below is the static structure in C ++. How can this be represented in 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

+4
source share
5 answers

In java, you can create a collection / array of Pair objects or use a 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) ..... } 
+3
source

There is no static structure. 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 C ++ grammar allows a type definition to replace the type name in a variable declaration.

Perhaps you know how to convert these two independent parts in Java?

+2
source

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)) ); 
+2
source

There are no structures in Java. You use classes in the same way.

In this case, you must have a class for the data structure that you intend to store.

One of many possible implementations:

 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 one array of pairs as a static variable for a particular class, and you can define a static array class of these DataStructure objects, consisting of 2 integers, such as yo udefined or consisting of everything you want if you define a class by to another.

0
source

There is no direct translation in Java.

Instead of a structure, you can use the inner class, assuming that you plan to change the fields of each element of the array.

To closely model the semantics of c / C ++, you can make member variables a public scope.

If you intend to use them read-only, you can make them final . Or you can even go as far as defining an enumeration for this dataset if the number of elements is also fixed.

There is not a very good way to reduce the "ceremony" of writing a new Pair(...) group. When all the fields are of the same type, you can write a factory method that takes n-elements of an array x n-fields parameters ... but you lose some verification of the compilation time.

0
source

All Articles