好吧,這只是一個黑暗中的鏡頭,但它可能是導致大部分錯誤獲得的原因。初始化變量的正確方法
當你初始化的東西。可以說一個小型的揮杆計劃。它會是這樣的
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