好吧,这只是一个黑暗中的镜头,但它可能是导致大部分错误获得的原因。初始化变量的正确方法
当你初始化的东西。可以说一个小型的挥杆计划。它会是这样的
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];
}
}
我知道这可能是一个庞大的初学者的问题,但是这是得到的代码IVE太习惯。即时初始化三四次没有意义,因为我不知道他们在哪里gp。任何人都可以对此有所了解吗?
p.s.对于混乱的示例代码感到抱歉。我尽力了。
确定这里有一点更清楚。
代码的总体布局是什么我问。
我的代码格式化为这样。
变量; 构造函数; 方法;
,我会是正确的说法应该是这样的
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;
}
}
IM试图找出初始化变量的正确方法。因为即时定义他们每次我使用它们
你的问题对我来说不是很清楚。什么是你收到的错误信息? – CoolBeans 2010-04-26 16:25:21
这是你的代码或只是一个片段?很难看到你的代码实际上在做什么,因此我们很难帮助你。 – 2010-04-26 16:28:03
你是问“这是否会像这样”,而不是一个特定的方式,或只是一般?如果只是一般情况下,这些问题之一是没有更多信息就无法回答的问题;没有一个正确的方法来初始化每个程序。 – Pops 2010-04-26 16:36:17