dongxie3352 2010-03-24 14:55
浏览 58
已采纳

如何在Java中执行此MutliArray设置?

I come from a PHP background and I'm just getting my teeth into some Java. I was wondering how I could implement the following in Java as simply as possible, just echoing the results to a terminal via the usual "System.out.print()" method.

<?php
$Results[0]['title'] = "No Country for Old Men";
$Results[0]['run_time'] = "122 mins";
$Results[0]['cert'] = "15";
$Results[1]['title'] = "Old School";
$Results[1]['run_time'] = "88 mins";
$Results[1]['cert'] = "18";

// Will basically show the above in order.
foreach($Results as value) {
     echo $Results[$value]['title'];
     echo $Results[$value]['run_time'];
     echo $Results[$value]['cert'];
}

// Lets add some more as I need to do this in Java too

$Results[2]['title'] = "Saving Private Ryan";
$Results[2]['run_time'] = "153 mins";
$Results[2]['cert'] = "15";

// Lets remove the first one as an example of another need
$Results[0] = null;
?>

I hear there are "list iterators" or something that are really good for rolling through data like this. Perhaps it could be implemented with that?

A fully working .java file would be most handy in this instance, including how to add and remove items from the array like the above.

P.S. I do plan on using this for an Android App in the distant future, so, hopefully it should all work on Android fine too, although, I imagine this sort of thing works on anything Java related :).

  • 写回答

2条回答 默认 最新

  • doutui4649 2010-03-24 15:06
    关注

    If I were doing that in Java I would make the following changes:

    1. Make a Movie class that encapsulates the data about a movie
    2. Use Lists instead of native arrays

    A Movie class may look something like this:

    public class Movie {
    
        public final String title;
        public final int runtime;
        public final int cert;
    
        public Movie(String title, int runtime, int cert) {
            this.title = title;
            this.runtime = runtime;
            this,cert = cert;
        }
    }
    

    Then you could make a List of Movies like so:

    List<Movie> movies = new ArrayList<Movie>();
    movies.add(new Movie("Some Movie", 120, 15);
    movies.add(new Movie("Another Movie", 90, 18);
    

    You can remove Movies like so:

    movies.remove(0);
    

    And you can iterate through them like so:

    for (Movie m: movies) {
        System.out.println("Title: " + m.title + 
                           ", Runtime: " + m.runtime + 
                           ", Cert: " + m.cert);
    }
    

    If you wanna get specific things out of the List, you can do the following:

    Movie the2ndOne = movies.get(1);
    System.out.println("Title: " + the2ndOne.title); // or
    System.out.println("Title: " + movies.get(0).title);
    

    To view an entire List simply do:

    System.out.println(movies.toString());
    

    Note: This assumes that the Movie class has an intelegent toString() method written for it.

    For some more reference on Lists that I showed above, check out the javadocs on them.

    When writing in a OO language, you should use OO principles.

    It may also be helpful to read this question and my answer to it. It is very similar in that you two are both trying to do things with arrays that would better be served using classes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题