Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中表示静态结构 是C++中的静态结构。如何用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}};_Java_C++ - Fatal编程技术网

如何在Java中表示静态结构 是C++中的静态结构。如何用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}};

如何在Java中表示静态结构 是C++中的静态结构。如何用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}};,java,c++,Java,C++,谢谢在Java中没有结构。您以类似的方式使用类 在这种情况下,您应该为要保留的数据结构创建一个类 许多可能的实施方案之一是: 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(i

谢谢

在Java中没有结构。您以类似的方式使用类

在这种情况下,您应该为要保留的数据结构创建一个类

许多可能的实施方案之一是:

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

}


}

然后,您可以使用一个对数组作为特定类的静态变量,您可以让您的类定义这些数据结构对象的静态数组,由两个整数组成,每个整数都像yo udefined一样,或者由您想要的任何内容组成,如果您以不同的方式定义类。

在java中,您可以创建成对对象的集合/数组或使用多维数组(数组数组数组)

没有“静态结构”。您所拥有的相当于:

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}
};

和C++语法允许类型定义替换变量声明中的类型名称。


您可能知道如何将这两个独立的部分转换为Java?

Java中没有直接翻译

假设您计划修改每个数组元素的字段,则可以使用内部类代替结构

要对c/c++语义进行紧密建模,可以将成员变量
设置为public
范围

如果您希望这些文件是只读的,可以将其设置为
最终版
。或者,如果元素的数量也是固定的,您甚至可以为此数据集定义枚举


没有一个很好的方法来减少编写一堆新的
对(…)
的“仪式”。当所有字段都是相同类型时,您可以编写一个工厂方法,该方法采用参数的n元素x n字段数组。。。但是您会丢失一些编译时正确性检查。

这可能是(惯用的)Java最好的方法:

最终类对{
第一次公开决赛;
公开决赛B秒;
专用对(A第一,B第二){
this.first=first;
这个秒=秒;
}      
公共静态对(A第一,B第二){
返回新的一对(第一,第二);
}
}
List pairs=Arrays.asList(
asList(成对的(32343235),成对的(56785679)),
数组.asList(成对的(34563457),成对的(23672368))
);

的确如此。但这并没有解决静态初始化问题。是的,我应该也把它放在示例中。静态数组的初始化应按如下方式进行:静态数据结构[]数组=新数据结构[]{new DataStructure(37703780),new DataStructure(3770.3781),…}将
静态变量定义放在哪里?静态变量正好位于变量类型之前。例如,私有静态int[][]对=。。。。。或私有静态对象someObj;这是推荐的做法。语言规范实际上允许C++中大多数相同的自由度。(大多数,不是全部。你仍然必须把所有的修饰符放在类型之前,像
unsigned static int
这样的东西在Java中是不合法的。)啊,我的意思是,整个变量定义在哪里?在这种情况下,你把它放在任何方法之外,因为它是类级变量。所以在公共类MyClass行和任何方法之前。
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) ..... }
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}
};
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))       
);