简体   繁体   English

简化Java数组布局

[英]simplifying java array layout

is there any other way of applying this table to array rather than creating 31 of them? 还有什么其他方法可以将此表应用于数组而不是创建31个表?

The layout looks like: 布局看起来像:

state_name|year|crimetype1|crimetype2|place|count| state_name | year | crimetype1 | crimetype2 | place | count |


row1: state1|2001|murder|knife|home|5| 第1列:state1 | 2001 |谋杀|刀|家| 5 |

row2: state1|2001|murder|axe|home|2| 第2列:state1 | 2001 |谋杀|斧头|家| 2 |

row3: state2|2001|robbery|baseball|shop|1| 第3列:state2 | 2001 |抢劫|棒球|商店| 1 |

and so on for 31 different states. 以此类推,适用于31个不同的州。

I thought of creating 31 arrays for each state with 5 rows but is there any other way of making it simpler? 我想到了为每个状态创建5行5个31数组,但是还有其他方法可以简化它吗?

Simply put: objects. 简单地说:对象。 That way, you will not have to have nested arrays. 这样,您将不必具有嵌套数组。

Make a class: 上课:

public class Crime{
    private int year;
    private String state;
    private String crimeType1, crimeType2;
    private String place;
    private int count;

    //And then you'd have some useful stuff here...
}

Besides this, I don't think you should combine the 31 values anymore - they are 31 separate, independent occurrences. 除此之外,我不认为您应该再合并31个值-它们是31个独立的独立出现。

First, create a Crime object which can represent a single row in that collection. 首先,创建一个Crime对象,该对象可以表示该集合中的一行。 Let's make it naturally comparable against itself based on the state. 让我们根据状态自然地使其与自身可比。

public class Crime implements Comparable<Crime> {
    private String state;
    private Year year;
    private String crimeType1;
    private String crimeType2;
    private String place;
    private int count;

    public int compareTo(Crime other) {
        return state.compareTo(other.state);
    }
}

I leave the construction of this object as an exercise for the reader. 我将这个对象的构造留给读者练习。

Next, you'll want to add the results of that value to a list. 接下来,您需要将该值的结果添加到列表中。

List<Crime> crimes = new ArrayList<>();

// In a loop reading the file
    crimes.add(crime);

At this point your work is pretty much done; 至此,您的工作已经完成。 if you want to get the crimes by a specific state, you can use the Stream API to filter based on that. 如果您想按特定状态获取犯罪,则可以使用Stream API对此进行过滤。

It is better to create class Crime as follows getHeader() returns header and getRow will return the row . 最好按照以下方法创建犯罪类:getHeader()返回header,而getRow将返回row。 (or toString) (或toString)

public class Crime {

    String stateName;
    String year;
    //...

    public static String getHeader(){
        //you can improve this
        return "state_name|year|crimetype1|crimetype2|place|count|";
    }

    public String getRow(){        
        return stateName + "|" + year +"|" ...
    }

    public static void main(String[] args) {
        List<Crime> state  = new ArrayList<>(31);
        //or 
        List<Crime> state = Arrays.asList(new Crime[] {
           new Crime("ST","2004",..),... 
        });
    }
}

as KevinO pointed if you are coding OO language it is good to think in OO way otherwise there no point of using java . 正如KevinO指出的,如果您正在编码OO语言,最好以OO方式进行思考,否则就没有使用java

Create a Crime class with all the values except state and then a Map 使用除state之外的所有值创建一个Crime类,然后创建一个Map

Map<String,ArrayList<Crime>> multiMap = new HashMap<String,ArrayList<Crime>>();

Here String is state name and the ArrayList for all the crimes in that state, So at the end there will be 31 keys (Here we are using state as unique keys) in muliMap Object . 这里String是状态名称,是该状态下所有犯罪的ArrayList ,所以最后在muliMap Object中将有31个键(这里我们将状态用作唯一键)。

So later you no need to filter statewise crimes, all you need to do is 因此,以后您无需过滤州级犯罪,您所需要做的就是

ArrayList<Crime> state1Crimes = multiMap.get("state1");

Simple and straight forward. 简单直接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM