How to create a new Jagged Array given other arrays? Is there a method to delete an element from an array?

StackOverflow https://stackoverflow.com/questions/14968543

  •  10-03-2022
  •  | 
  •  

Pergunta

public class Yearly { 

    public void getYearlyData() throws FileNotFoundException  {

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

I have an array: String[] state, which contains the names of the states specified by the user.

It may look like this: String[] state = {"Illinois", "Kansas", "Wyoming"};

The following is the two dimentional jagged array. The first subelement of each element is the name of the state, then there are names of the Newspapers in that state.

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

The goal is to create a new jagged array that would include only those elements of statePapersInitial array that contain the names of the states that have beenchoosen by the user in the state array.

I called the new array statePapers

I have tried the following:

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;
    }
}

However, it does not work, it throws an Exception in thread "main"

java.lang.NullPointerException
at Yearly.getYearlyData(Yearly.java:95)
at Main.main(Main.java:9)
Foi útil?

Solução

Using just arrays:

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;
  }
}

the output is:

Alabama BGNB 
Wyoming WTEB WYOM WMPP 

Now an approach that uses Maps might look like this:

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;
  }
}

The output of this is:

Wyoming=WTEB WYOM WMPP 
Alabama=BGNB 

Here you can see that it is ignoring the "NOT A STATE" that was passed, and it is using the State name as a key as opposed to the first element in an array.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top