Java的:阵列,放大器;矢量 [英] Java: Arrays & Vectors

查看:117
本文介绍了Java的:阵列,放大器;矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经习惯了与PHP的工作,但最近我一直在使用Java和我有一个头痛试图弄清楚这一点。我想保存在Java的这种重新presentation:

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"] 
                          )
)

有没有干净的方式(即没有脏code)保存在Java中这件事情?注意;我想用字符串作为数组索引(第一维),我不知道该数组的大小确定。

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).

有关详细信息,请检查导航教程和列表,也许你应该用<一开始HREF =htt​​p://java.sun.com/docs/books/tutorial/collections/相对=nofollow>集合教程。

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的:阵列,放大器;矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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