2013-01-31 2 views
1

저는 원자의 부식을 나타내는 프로그램을 만들고 있습니다. 여기에는 내 논리가있다. 그러나 다른 클래스에서 명확하게 정의 된 경우 정의되지 않은 생성자 오류가 발생합니다. 왜 이런 일이 일어나는 걸까요?명확하게 정의 된 경우 생성자가 정의되지 않았습니다.

주의 : 표기하지 않았습니다. 너의 진노를 용서해주십시오.

import java.util.Random; 
import java.awt.*; 
import javax.swing.*; 
import javax.swing.JFrame; 

public class Main { 
    public static void main(String args[]) { 
     int chance = 6; 
     Random r = new Random(); 
     int num = 40; 
     int[] decayed; 
     int reps = 25; 
     decayed = new int[reps]; 
     for (int j = 1; j < reps+1; j++) { 
      for (int i = 0; i < num; i++) { 
       int c = r.nextInt(chance); 
       if (c == chance - 1) { 
        decayed[j]++; 
       } 
      } 
      System.out.printf("\n Trial: " + j + "\n Number left: " + num 
        + "\n Decayed: " + decayed[j] + "\n\n"); 
      num = num - decayed[j]; 

     } 
     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.getContentPane().add(new Graph(decayed[])); //"Constuctor is undefined for type int" When I am clearly specifying an array. 
     f.setSize(400,400); 
     f.setLocation(200,200); 
     f.setVisible(true); 
    } 
} 

내 Graph.class. 그것은 몇몇 공개 토론 (신용에 크리그 우드)에서 복사됩니다.

import java.awt.*; 
import javax.swing.*; 
import java.util.*; 
public class Graph extends JPanel 
{ 
    int PAD = 20; 
    boolean drawLine = true; 
    boolean drawDots = true; 
    int dotRadius = 3; 

    // the y coordinates of the points to be drawn; the x coordinates are evenly spaced 
    int[] data; 
    public Graph(int points[]){ //This is the constructor which specifies type int[]. 
     for (int i = 0; i<points.length; i++){ //Copies points[] to data[] 
      data[i] = points[i]; 
     } 
    } 

    protected void paintComponent (Graphics g) 
    { 
     super.paintComponent(g); 
     Graphics2D g2 = (Graphics2D)g; 
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     int w = getWidth(); 
     int h = getHeight(); 
     g2.drawLine(PAD, PAD, PAD, h-PAD); 
     g2.drawLine(PAD, h-PAD, w-PAD, h-PAD); 
     double xScale = (w - 2*PAD)/(data.length + 1); 
     double maxValue = 100.0; 
     double yScale = (h - 2*PAD)/maxValue; 
     // The origin location 
     int x0 = PAD; 
     int y0 = h-PAD; 

     // draw connecting line 
     if (drawLine) 
     { 
      for (int j = 0; j < data.length-1; j++) 
      { 
       int x1 = x0 + (int)(xScale * (j+1)); 
       int y1 = y0 - (int)(yScale * data[j]); 
       int x2 = x0 + (int)(xScale * (j+2)); 
       int y2 = y0 - (int)(yScale * data[j+1]); 
       g2.drawLine(x1, y1, x2, y2); 
      } 
     } 

     // draw the points as little circles in red 
     if (drawDots) 
     { 
      g2.setPaint(Color.red); 
      for (int j = 0; j < data.length; j++) 
      { 
       int x = x0 + (int)(xScale * (j+1)); 
       int y = y0 - (int)(yScale * data[j]); 
       g2.fillOval(x-dotRadius, y-dotRadius, 2*dotRadius, 2*dotRadius); 
      } 
     } 
    } 
} 
+4

이 [패 : 당신이 잘못되지 않았다하더라도, 다시 쓰기 생성자는 자바 표준과 규칙을 유지하기 위해 고려하시기 바랍니다

f.getContentPane().add(new Graph(decayed)); 

:

시도는 전화를 다시 쓰기 ] from decayed [] ' – Thomas

+0

정말로'정의되지 않은 '오류가 발생하고 있습니까? 구문 오류가 발생합니다. 똑같은 것이 아닙니다. – EJP

+0

http://sscce.org/을 확인하십시오. –

답변

4

귀하의 구문은 []하지 않고, 단순히 변수 이름을 사용하여 배열을 참조하는, 무효 : 그냥이

f.getContentPane().add(new Graph(decayed));f.getContentPane().add(new Graph(decayed[]));

을 대체

f.getContentPane().add(new Graph(decayed)); 
1

그냥 []없이 생성 한 변수의 이름을 사용하십시오.

대괄호는 배열의 메서드 매개 변수를 선언 할 때만 사용되며 메서드를 호출 할 때는 사용되지 않습니다.

8

여기의 문제는 해당 [] 괄호를 사용하는 경우입니다.

public Graph(int[] points){ // NOTE: I moved the [] to a the standard position 
    for (int i = 0; i<points.length; i++){ 
     data[i] = points[i]; 
    } 
} 
+1

@ theJollySin'[] '이 양쪽에 허용됩니다. 왜 대답을 언급합니까? – Abubakkar

+5

@Abu 저를 용서할 수있게 용서하지만, 표준과 언어 관습은 모든 사람의 삶을 편하게 만듭니다. – theJollySin

+0

네, 맞습니다. 그렇다면 OP가 자신의 솔루션에서 OP가 사용한 방식이 잘못되었다고 생각할 수도 있습니다. 오히려 당신은 협약으로 언급 했어야합니다 (분명하게 지적했습니다) – Abubakkar

관련 문제