Java:数组&载体 [英] Java: Arrays & Vectors

查看:148
本文介绍了Java:数组&载体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我习惯于使用PHP,但最近我一直在使用Java,而我正在努力解决这个问题。我想在Java中保存此表示:

I'm used to working with PHP but lately I've been working with Java and I'm having a headache trying to figure this out. I want to save this representation in Java:


Array ( 
        ["col_name_1"] => Array ( 
                               1 => ["col_value_1"], 
                               2 => ["col_value_2"], 
                               ... , 
                               n => ["col_value_n"] 
                          ),
        ["col_name_n"] => Array ( 
                               1 => ["col_value_1"], 
                               2 => ["col_value_2"], 
                               ... , 
                               n => ["col_value_n"] 
                          )
)

有没有一个干净的方式(即没有脏代码)来保存这个东西在Java中?注意;我想使用Strings作为数组索引(在第一维),我不知道数组的确定大小。

Is there a clean way (i.e. no dirty code) to save this thing in Java? Note; I would like to use Strings as array indexes (in the first dimension) and I don't know the definite size of the arrays..

推荐答案

您可以使用地图和列表(这些都是以多种方式实现的界面,您可以选择最适合您的情况)。

You can use a Map and a List (these both are interfaces implemented in more than one way for you to choose the most adequate in your case).

有关更多信息,请查看地图列表,也许您应该从收藏教程开始。

For more information check the tutorials for Map and List and maybe you should start with the Collections tutorial.

一个例子:

import java.util.*;

public class Foo {
    public static void main(String[] args) {
        Map<String, List<String>> m = new HashMap<String, List<String>>();
        List<String> l = new LinkedList<String>();
        l.add("col_value_1");
        l.add("col_value_2");
        //and so on
        m.put("col_name_1",l); //repeat for the rest of the colnames

       //then, to get it you do

       List<String> rl = m.get("col_name_1");

    }
}

这篇关于Java:数组&amp;载体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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