java字符串数组占用多少空间?更具体地说,字符串数组看起来像这样的空间(以字节为单位):
Longbowman
kingdom
lord
weapon1,weapon2,weapon3
armor1,armor2,armor3
Run Code Online (Sandbox Code Playgroud)
我问这个是因为我要创建一个包含5,000多个这些数组的程序,并想知道它将占用多少空间,所以我知道是否应该重做数据存储.
那么回顾一下,字符串数组(如果可量化的话)占用了多少空间?
我有2节课.1个类传递一个ImageIcon [],另一个类获取数组并保存它.
这些是每个类的减少版本:
Class 1:monsterLabel []是一个ImageIcon [] saveLoad是Class2的名称
ImageIcon[] toSave= new ImageIcon[button.length];
toSave[i]= new ImageIcon();
for (int i = 0; i < 99; i++) {
toSave[i]=(ImageIcon) monsterLabel[i].getIcon();
}
saveLoad.saver(toSave[]);
Run Code Online (Sandbox Code Playgroud)
等级2(saveLoad)
public void saver(ImageIcon[] buttonPic) {}
Run Code Online (Sandbox Code Playgroud)
问题出现在第1类,Eclipse告诉我
"Syntax error on token "[", Expression expected after this token"
Run Code Online (Sandbox Code Playgroud)
在哪里我传递给保存[]
当我输入一个数字即.toSave [0],它告诉我class2正在寻找一个ImageIcon []而不是ImageIcon.我怎样才能传递数组?
如何在java中传递数组维度和数组?我这样的东西:
public String[1][1] abc(){
return theStringArray
}
//but this isn't possible
Run Code Online (Sandbox Code Playgroud)
那么有没有办法传递数组的维度?
现在我有2个方法为每个维度传递一个int但是有更好的方法吗?
它的核心问题是:
当我尝试传递数组并找到它的长度时,它给了我一大堆错误.问题是接收数组的类的数组需要初始化传递的数组将复制到的数组,但是没有传递的数组维度,我该如何初始化它?
数组传递给的类:
String[][] lordStats;
ArrayList<String> troopList;
public void loader() {
lord lorder = new lord();
lordStats = lorder.returnLord();
total = lorder.returnLordTotal();
for (int i = 0; i < lordStats[0].length; i++)
troopList.add (lordStats[i][2]);
}
Run Code Online (Sandbox Code Playgroud)
数组的类来自:注意方法主创建者被称为多次.
public class lord {
static int total;
String[][] lordStats;
public void total(int total1) {
total = total1;
System.out.println("lordTotal");
}
public void lordCreator(String lord, String kingdom, String troop, int times) {
lordStats = …Run Code Online (Sandbox Code Playgroud) 在我的空闲时间里,我一直在学习Java,到目前为止,我对如何正确使用变量一无所知.我的意思是,我应该更喜欢使用实例变量,还是只使用单个方法中的变量.
例如,这是我写的一段代码:
public class arsenalTroop {
String[][] troopStats;
String[][] weaponStats;
String[][] armorStats;
String[][] animalStats;
String[] troops;
String[] weapon;
String[] armor;
String[] animal;
JLabel[] troopsArray;
int troopTotal;
int weaponTotal;
int armorTotal;
int lordTotal;
int animalTotal;
JFrame arsenalLordFrame = new JFrame();
JTextField name = new JTextField();
JLayeredPane lP = new JLayeredPane();
JLayeredPane fP = new JLayeredPane();
JLayeredPane ldP= new JLayeredPane();
JLabel siegeLabel = new JLabel();
JComboBox weaponCB;
JComboBox armorCB;
JComboBox animalCB;
JLabel siegeText = new JLabel();
JButton addWep = new JButton();
JButton …Run Code Online (Sandbox Code Playgroud) 我看过很多网站.如果没有面板,标签会正确显示,面板会显示错误:
Exception in thread "main" java.lang.NullPointerException
Run Code Online (Sandbox Code Playgroud)
那么我该怎么做才能解决这个问题呢?
这是源代码:
JLabel button[] = new JLabel[100];
JPanel[] panel = new JPanel[100];
for (int i = 0; i < button.length; i++) {
a = a + 50;
if (a > 549) {
b = b + 50;
a = 50;
}
button[i] = new JLabel("hi");
frame.add(button[i]); //is this necessary?
button[i].setVisible(true); // is this necessary?
button[i].setSize(50,50);
panel[i].add(button[i]);
panel[i].setVisible(true);
panel[i].setBounds(a, b, 50, 50);
frame.add(panel[i]);
}
Run Code Online (Sandbox Code Playgroud)
这有什么不对,怎么解决呢?只是你知道,它应该有100个标签,在10乘10阵列中说你好.这是它的样子:

你如何if...else在for声明中发表声明?
这是代码:
// This is within a actionListener. When ever I press a button, this happens.
for (int i = 0; i < booleanVariable.length; i++) {
//length is 8
System.out.println("booleanVariable is" + booelanVariable[i]);
if (booleanVariable[i] = true) {
booleanVariable[i] = false;
System.out.println("booleanVariable was true")
break;
} else {
System.out.println(" booleanVariable is false");
}
}
Run Code Online (Sandbox Code Playgroud)
这是3台印刷机的输出:
booleanVariable is true
booleanVariable was true
//correct
booleanVariable is false
booleanVariable was true
//incorrect.
booleanVariable is false
booleanVariable was true
//incorrect …Run Code Online (Sandbox Code Playgroud) java ×6
arrays ×3
swing ×2
coding-style ×1
data-storage ×1
dimensions ×1
for-loop ×1
if-statement ×1
jpanel ×1
methods ×1
variables ×1