2012-02-10 6 views
0

C++에서 Java로 변환하고 있습니다. 누구든지 Java로 변환하는 방법을 알고 있습니까?Java Array를 한 줄로 초기화하십시오.

typedef struct { 
     int id;     
     char *old_end;   
     char *new_end;   
     int old_offset;  
     int new_offset;  
     int min_root_size;  
    int func;   
     } RuleList; 

static RuleList step1a_rules[] = 
     { 
     101, "sses",  "ss", 3, 1, -1, _NULL, 
     102, "ies",  "i",  2, 0, -1, _NULL, 
     103, "ss",  "ss", 1, 1, -1, _NULL, 
     104, "s",   _LAMBDA, 0, -1, -1, _NULL, 
     000, NULL,  NULL, 0, 0, 0, _NULL, 
     }; 

당신은 RuleList에 대한 생성자가 필요합니다

+2

* "한 줄에"* 소스 코드의 주된 목적이 사람이 읽을 수 있어야한다는 것은 꽤 임의적입니다. BTW - 질문에 물음표를 추가하는 것을 잊지 마십시오. 질문을 직접 풀기 위해 돈을 지불해야합니다. –

+0

배열의 타입이'RuleList' 인 경우, 그 배열은'RuleList' 객체로 초기화해야합니다. 정의에 리터럴을 넣을 수 없습니다. 또한 구조체를 반영하는 Java 클래스를 만들어야합니다. –

+0

여기에 많은 괄호가 없습니다. 정확히 말하면 모든 행 주위에. – Xeo

답변

1

감사 :

class RuleList { 
    int id;     
    String old_end;   
    String new_end;   
    int old_offset;  
    int new_offset;  
    int min_root_size;  
    int func; 

    RuleList(int id, String old_end, String new_end, int old_offset, 
     int new_offset, int min_root_size, int func) 
    { 
     this.id = id; 
     this.old_end = old_end; 
     // etc. 
    } 
}; 

static RuleList[] step1a_rules = { 
    new RuleList(101, "sses",  "ss", 3, 1, -1, _NULL), 
    new RuleList(102, "ies",  "i",  2, 0, -1, _NULL), 
    new RuleList(103, "ss",  "ss", 1, 1, -1, _NULL), 
    new RuleList(104, "s",   _LAMBDA, 0, -1, -1, _NULL), 
    new RuleList(000, null,  null, 0, 0, 0, _NULL), 
}; 

_NULL 정의 된 정적 int 값이고 _LAMBDA 정의 된 정적 String 값이라고 가정합니다.

+1

Java에서 변수 이름에 밑줄을 사용하지 않겠습니다. 또한, "new RuleList []"가 누락 되었기 때문에 아래 줄은 컴파일되지 않습니다. static RuleList [] step1a_rules = { –

+0

실제로 최근 Java 버전에서는 이전에 새 RuleList []를 생략 할 수 있다고 생각합니다. '{'...? 아니면 예를 들어 ints. –

+0

@Ben - 방금 OP의 변수 이름을 재사용하고있었습니다. 나는 그들이 보통 자바 스타일이 아니라는 것에 동의한다. 마지막 문장은 완벽하게 합법적입니다 : 그것은 새로운 RuleList []를 필요로하지 않는 배열 이니셜 라이저입니다. ([Java 언어 사양, 섹션 10.6] (http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#10.6)을 참조하십시오). –

1

Java에서이 작업을 수행하는 표준 방법은 다음과 같습니다. Java의 RuleList 클래스는 RuleList C 구조체보다 약간 자세한 정보가 있지만 Eclipse를 사용하여 대부분의 코드를 생성하는 등의 방법으로 쉽게 처리 할 수 ​​있습니다. 즉,이 클래스의 배열을 만든 후

public class RuleList { 
    private final int id;     
    private final String oldEnd;   
    private final String newEnd;   
    private final int oldOffset;  
    private final int newOffset;  
    private final int minRootDize;  
    private final int func; 

    public RuleList(int id, String oldEnd, String newEnd, int oldOffset, 
     int newOffset, int minRootDize, int func) { 
    this.id = id; 
    this.oldEnd = oldEnd; 
    this.newEnd = newEnd; 
    this.oldOffset = oldOffset; 
    this.newOffset = newOffset; 
    this.minRootDize = minRootDize; 
    this.func = func; 
    } 

    public int getId() { 
    return id; 
    } 
    public String getOldEnd() { 
    return oldEnd; 
    } 
    public String getNewEnd() { 
    return newEnd; 
    } 
    public int getOldOffset() { 
    return oldOffset; 
    } 
    public int getNewOffset() { 
    return newOffset; 
    } 
    public int getMinRootDize() { 
    return minRootDize; 
    } 
    public int getFunc() { 
    return func; 
    } 
} 

RuleList[] step1aRules = new RuleList[] { 
    new RuleList(101, "sses",  "ss", 3, 1, -1, 0), 
    new RuleList(102, "ies",  "i",  2, 0, -1, 0), 
    new RuleList(103, "ss",  "ss", 1, 1, -1, 0), 
    new RuleList(104, "s",   _LAMBDA, 0, -1, -1, 0), 
    new RuleList(000, null,  null, 0, 0, 0, 0), 
}; 
+2

왜 final for variable을 사용하는지 말해 줄 수 있습니까? 그것은 위의 C++ 코드와 관련이 있다는 것을 의미합니까? 감사. – hqt

+1

이것은 RuleList를 생성 한 후에 변수 값을 변경하지 않을 계획임을 의미합니다. 값을 변경할 수있게하려면 final을 제거하고 setter를 다음과 같이 추가해야합니다. public void setId (int id) {this.id = id; } –

+1

'새로운 RuleList []'는 마지막 문장에서 불필요하다. 또한 멤버 변수가 final로 선언 되었기 때문에 멤버 변수를 공개하고 접근 자 함수를 사용하지 않아도됩니다. –

0

당신은, 클래스 RuleList를 만들 수 있습니다 위의 코드에서

public class RuleList{ 
    int id ;   
    char old_end; //note that java does not use (* for pointer like C++)  
    char new_end;   
    int old_offset;  
} 

static RuleList[] step1a_rules = new RuleList[]{ 
        new RuleList (101,"sses","ss",3, 1,-1, 0),... }; 

, 당신은 C에서 뭔가 다른 것을 볼 수 ++ 자바, 거의 모든 개체가 있기 때문이다. 그래서 step1a_rules 선언은 다른 객체 (Rulelist)를 포함하는 배열입니다. 따라서이 클래스의 각 객체는 새 객체를 인스턴스화 할 때 new 키워드를 사용해야합니다.

@ 편집 : 아, 그리고 거의 잊어 버렸습니다. 자바와 C++은 약간 다릅니다. C++ 클래스에서 기본 멤버는 private입니다. Java 클래스에서 기본 멤버는 public입니다. 물론 struct의 기본 멤버도 public이기 때문에 위의 코드는 아무런 문제가되지 않습니다. D

+0

) Java 클래스에서 기본 액세스 권한은 _package private_ : 동일한 패키지에 정의 된 코드에만 액세스 할 수 있으며 마지막 문장에는 새 RuleList []가 필요하지 않으며, 이것을 시도 했습니까? 인수를 허용하는'RuleList' 생성자를 명시 적으로 선언해야합니다. –