1
public class Yearly { 

    public void getYearlyData() throws FileNotFoundException  {

        String[] state = StatesList.getValues();
        int numberOfStates = state.length;

我有一个数组:String[] state,其中包含用户指定的状态的名称。

它可能看起来像这样:String[] state = {"Illinois", "Kansas", "Wyoming"};

下面是二维的锯齿状数组。每个元素的第一个子元素是州名,然后是该州的报纸名称。

String statePapersInitial[][] = {
                { "Alabama", "BGNB"},
                { "Alaska", "ADNB", "ALKP"},
                { "Arizona", "ADSB" },
                ..................//Total = 51.................
                { "Wyoming","WTEB", "WYOM", "WMPP"};

目标是创建一个新的锯齿状数组,该数组将仅statePapersInitial包含数组中包含用户在state数组中选择的状态名称的那些元素。

我调用了新数组statePapers

我尝试了以下方法:

String[][] statePapers = null;  
int x = 0;
for (x = 0; x < state.length; x++)
{
    int i = 0;
    for (i = 0; i < statePapersInitial.length; i++)
    {   
        if (state[x].equals(statePapersInitial[i][0]))
        {
            int j = 1;
            for (j = 1; j < statePapersInitial[i].length; j++)
                statePapers[x][j-1] = statePapersInitial[i][j];
            System.out.println(statePapers[x][j-1]);
        }
        else;
    }
}

但是,它不起作用,它在线程“main”中引发异常

java.lang.NullPointerException
at Yearly.getYearlyData(Yearly.java:95)
at Main.main(Main.java:9)
4

1 回答 1

0

仅使用数组:

public class q14968543 {
  public static void main(String[] args) {
    String statePapersInitial[][] = {
      { "Alabama", "BGNB" },
      { "Alaska",  "ADNB", "ALKP" },
      { "Arizona", "ADSB" },
      { "Wyoming", "WTEB", "WYOM", "WMPP" }
    };

    for(String[] papers : selectOnly(statePapersInitial, "Alabama", "Wyoming")) {
      for(String paper : papers) {
        System.out.print(paper+ " ");
      }
      System.out.println();
    }
  }

  private static String[][] selectOnly(String[][] statePapers, String ... states) {
    String[][] selected = new String[states.length][0];
    int index = 0;
    for(String state : states) {
      for(String[] papers : statePapers) {
        if(state.equals(papers[0])) {
          selected[index++] = papers;
        }
      }
    }
    return selected;
  }
}

输出是:

Alabama BGNB 
Wyoming WTEB WYOM WMPP 

现在使用Maps 的方法可能如下所示:

import java.util.*;
public class q14968543 {
  public static void main(String[] args) {
    Map<String, List<String>> statePapers = new HashMap<>();
    statePapers.put("Alabama", Arrays.asList((new String[] {"BGNB"})));
    statePapers.put("Alaska",  Arrays.asList((new String[] {"ADNB", "ALKP"})));
    statePapers.put("Arizona", Arrays.asList((new String[] {"ADSB"})));
    statePapers.put("Wyoming", Arrays.asList((new String[] {"WTEB", "WYOM", "WMPP"})));

    for(Map.Entry<String, List<String>> state : selectOnly(statePapers, "Alabama", "NOT A STATE", "Wyoming").entrySet()) {
      System.out.print(state.getKey()+"=");
      for(String paper : state.getValue()) {
        System.out.print(paper+ " ");
      }
      System.out.println();
    }
  }
  private static Map<String, List<String>> selectOnly(Map<String, List<String>> statePapers, String ... states) {
    Map<String, List<String>> selected = new HashMap<>();
    for(String state : states) {
      if(statePapers.containsKey(state)) {
        selected.put(state, statePapers.get(state));
      }
    }
    return selected;
  }
}

这个的输出是:

Wyoming=WTEB WYOM WMPP 
Alabama=BGNB 

在这里你可以看到它忽略了"NOT A STATE"传递的那个,它使用状态名称作为键而不是数组中的第一个元素。

于 2013-02-19T22:55:02.453 回答