如何在java plz帮助中做对象2d数组 [英] how to do Objects 2d array in java plz help

查看:68
本文介绍了如何在java plz帮助中做对象2d数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我在此代码中遇到问题:



plz help i have problem in this code:

public static void onClick_Continue(JFrame newframe,structFile file_10_or_20[])
	{

Object[] columnNames = {"select", "precents", "file name", "number of words","source", "matchs","semi-matchs","status"};
		Object [][]data;
		if(file_10_or_20.length==10)
		{
		/*in thi line its begain ->*/	data={
				{false,"%%%", "broadcast.txt", "words", "source","mtachs","semi-matchs","image"},
				{false,"%%%", "broadcast.txt", "words", "source","mtachs","semi-matchs","image"},
				{false,"%%%", "broadcast.txt", "words", "source","mtachs","semi-matchs","image"},
				{false,"%%%", "broadcast.txt", "words", "source","mtachs","semi-matchs","image"},
				{false,"%%%", "broadcast.txt", "words", "source","mtachs","semi-matchs","image"},
				};
		}
		else {
                      data={
{"source","mtachs","semi-matchs","image"},
				{false,"%%%", "broadcast.txt", "words", "source","mtachs","semi-matchs","image"},};//do something
                      }};/*in this linr its end*/
}





但如果我这样做就可以了:





but if i do this its ok all good :

public static void onClick_Continue(JFrame newframe,structFile file_10_or_20[])
	{
		Object[] columnNames = {"select", "precents", "file name", "number of words","source", "matchs","semi-matchs","status"};
		Object[][] data = {
				{false,"%%%", "broadcast.txt", "words", "source","mtachs","semi-matchs","image"},
				{false,"%%%", "broadcast.txt", "words", "source","mtachs","semi-matchs","image"},
				{false,"%%%", "broadcast.txt", "words", "source","mtachs","semi-matchs","image"},
				{false,"%%%", "broadcast.txt", "words", "source","mtachs","semi-matchs","image"},
				{false,"%%%", "broadcast.txt", "words", "source","mtachs","semi-matchs","image"},
		};
}

推荐答案

有一个很大的区别:在第二个代码示例中,你隐式地初始化 Object [] [] 对象。在第一个中,它仍然是空的。然后,您尝试通过索引访问不存在的对象。



如果要在不填写值的情况下初始化数组,可以使用<$初始化它C $ C>新。等级1的情况最简单:

There is a big difference: in second code sample, you implicitly initialize the Object[][] object. In first one, it remains null. And then, you try to access non-existing object by indices.

If you want to initialize an array without filling in the values, you can initialize it using new. The case of the rank 1 is the simplest:
int length = //...
int[] myArray = new int[length];



排名2,你可以做类似


With rank 2, you can do something like

int[][] myArray = new int[10][20];



等等...

在更复杂的情况下,你应该明白这只是一个数组数组。所以,这个数组可以锯齿状:内部数组是外部数组的元素,它们可以有不同的长度。这是你只能初始化外部数组的方法:


And so on…
In more complex case, you should understand that this is just an array of arrays. So, this array can be jagged: inner arrays are the elements of the outer array, they can be of different lengths. This is how you can initialize only the outer array:

int[][] myArray = new [10][];



然后你可以遍历循环中的外部数组并将10个内部数组中的每一个初始化为您想要的长度,或者您可以在以后按需执行。你没有问过锯齿状的阵列,所以我会把它留给你,作为一个有用的家庭练习。如果出现问题,你可以在网上搜索很多代码样本。



另请注意对象是所有类的根类,因此,基本上,将它用作数组元素的类型与无类型数组相同。通常,您需要更专业的类型。也许您需要使用 polymorphism ,因为它在OOP中被理解,因此编译时元素类型可以是抽象类型的一些接口,并且运行时类型元素会有所不同。否则所有的多态性都将基于类型转换,这是对OOP的最基本违反。



-SA


Then you can traverse the outer array in a loop and initialize each of 10 inner arrays to the length you want, or you can do it later on demand. You did not ask about jagged arrays, so I would leave it for you, as a useful home exercise. If something goes wrong, you can do some Web search for very many code samples.

Also note that Object is the root class for all classes, so, essentially, using it as a type of an array element is the same as "untyped" array. Usually, you need more specialized type. Perhaps you need to use polymorphism, as it is understood in OOP, so the compile-time element type could be some interface of abstract type, and runtime types of elements would be different. Otherwise all your polymorphism would be based on type cast, which is a most basic violation of OOP.

—SA


您需要在一个语句中声明并将项添加到数组中,如第二个示例所示。在第一个示例中,您没有为数组分配任何空间,因此您无法向其中添加项目。
You need to declare and add the items to the array in one statement as shown in the second example. In the first example you have not allocated any space to the array so you cannot add items to it.


这篇关于如何在java plz帮助中做对象2d数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆