开发者

java multidimensional array declaration 1*3 dimension?

hi i am new to java.After so research about what i am facing, i try post to ask some question.

recently i am doing a text analyse software. and i try to get done with a 1*3 dimensional array. something like

[0]
   [][][]
[1]
   [][][]
[2] 
   [][][]
[3]
   [][][]

the three column in the second dimension of each is use for saving the detail开发者_如何学编程s of the first dimension. but the second dimension array size is yet unknown which mean that, idont know how many i will find from the text that i am gonna search.it will increase once the target found. is this the stupid way for doing this. i know java can declare array like int [][] abc = new [5][]. but it just can declare only for one unknown dimension. then i try to do something like this

String [] abc = new string [4]

then i first make a presumption that the size is that in the first column in the second dimension.

abc[0] = String [10][][] inside1;
abc[1] = String [10][][] inside2;
abc[2] = String [10][][] inside3;
abc[3] = String [10][][] inside4;

but still getting error when i compile it.

how can i do the declaration or there got better to done this easy. if i miss any post in the internet about this. please show me any keyword or link for me to take a look.


What is it that you are trying to implement? Sounds like you instead should use one of the collection classes together with value objects that represent your data.


I think i understand what you are trying to do and its like this:

                String[][] value = new String[4][3];

Java doenst have multidimensional arrays, its Arrays Within Arrays.


If you're trying to parse a text file and you know what each column signifies, you should create a new object which contains that data. Arrays of arrays are an unnecessarily painful hack and your code is much more maintainable if you just do what Java is designed to be used for--write a class.


Example for a 10x13 matrix:

String [] [] abd = new String [10] [13];

EDIT: I chose 10x13, because 1x3 doesn't make much sense, being the first value 1.


Why don't you create an Object that has a 'name' property (or 'index' if you prefer), and a 'list' property of type List?

public class YourMultiDimensionalArrayObject {
    private int index;
    private List<String> vals;

    public YourMultiDimensionalArrayObject(int _index) {
        index = _index;
    }

    public void setValues(List<String> _vals) {
        vals = _vals;
    }

    public int getIndex() {
        return index;
    }

    public List<String> getVals() {
        return vals;
    }
}


You can use ArrayList to store an array of int values of unknown length. You can use an ArrayList> to store an indefinite number of int arrays.


I would create either a List<List<String>> or a Map<String, List<String>>, assuming the values you want to store and look-up by are Strings.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜