【发布时间】:2010-04-26 16:17:58
【问题描述】:
好吧,这只是在黑暗中的一个镜头,但它可能是我得到的大多数错误的原因。
当你初始化某些东西时。让我们说一个小摇摆程序。会这样吗
variables here
{
private Jlist contactList;
String [] contactArray;
ArrayList <String> contactArrayList;
ResultSet namesList
// constructor here
public whatever()
{
GridLayout aGrid = new GridLayout(2,2,10,10);
contact1 = new String();
contact2 = new String();
contact3 = new String();
contactArrayList = new ArrayList<String>();
// is something supposed too go in the () of this JList?
contactList = new JList();
contactArray = new String[5];
from1 =new JLabel ("From: " + contactArray[1]);
gridlayout.add(components)// theres too many components to write onto SO.
}
// methods here
public void fillContactsGui()
{
createConnection();
ArrayList<String> contactsArrayList = new ArrayList<String>();
while (namesList.next())
{
contactArrayList.add(namesList.getString(1));
ContactArray[1] = namesList[1];
}
}
我知道这可能是一个巨大的初学者问题,但这也是我使用的代码。我也毫无意义地初始化了三四次,因为我不确定它们在哪里。任何人都可以对此有所了解吗?
附言对不起,凌乱的示例代码。我已经尽力了。
好吧,这里再清楚一点。
我要问的是代码的总体布局。
我的代码是这样格式化的。
变量; 构造函数; 方法;
我说它应该看起来像这样对吗
public class test
{
int i;
public test()
{
i = 0;
}
public void addi()
{
i = i +1;
}
}
不是这样的
public class test
{
int i = 0;
public test()
{
int i = 0;
}
public void addi()
{
int i = i +1;
}
}
我试图找出初始化变量的正确方法。因为我每次使用它们时都会定义它们
【问题讨论】:
-
你的问题对我来说不是很清楚。您收到的错误消息是什么?
-
这是你的代码还是只是一个sn-p?很难看到您的代码实际在做什么,因此我们很难为您提供帮助。
-
您是在问“会这样吗”,而不是特定的其他方式,还是一般而言?如果只是一般来说,这是没有更多信息就无法回答的问题之一;没有一个正确的方法来初始化每个程序。
-
抱歉不清楚。但我的真实代码非常糟糕。基本上,我在我的构造函数、变量部分(顶部)和我的方法中初始化了东西。我不确定我应该在哪里初始化它们。有更清楚的吗?
-
@Lord Torgamus.. 愿意解释一下吗?看到我认为所有的东西都以同样的方式初始化。该代码应该也比我拥有的代码清晰得多。抱歉,如果它令人困惑
标签: java swing initialization constructor