How to know how many positions an array has in Java?

Asked

Viewed 700 times

-2

Look at the picture; inserir a descrição da imagem aqui

I’m uploading a CSV file, and I’m already able to count how many lines there are, in our context are three lines as shown in the code below;

inserir a descrição da imagem aqui

However I need now and how to get the maximum number of positions, I’m not sure how to catch, I know that by doing a debug I can know that the maximum number of positions is 10 as shown in the figure below;

inserir a descrição da imagem aqui

I need to know how to create a line of code to get the amount of positions; this is my algorithm;

    File arquivoLeitura = new File(getArquivo());
LineNumberReader linhaLeitura = new LineNumberReader(new FileReader(arquivoLeitura));
linhaLeitura.skip(arquivoLeitura.length());
int qtdLinha = linhaLeitura.getLineNumber() + 1;

BufferedReader leitor = new BufferedReader(new InputStreamReader(new FileInputStream(getArquivo())));

String linha = null;

List<TotalIndicios> listaTotalDeIndicios = new ArrayList<TotalIndicios>();
List<IndiciosEntity> listaIndiciosEntity = new ArrayList<IndiciosEntity>();

for (int indiceDeIndicios = 1; indiceDeIndicios <= qtdLinha; indiceDeIndicios++) {

    linha = leitor.readLine();
    String[] dadosCSV = linha.split(VIRGULA);
    //vai fazer alguma coisa......
}
  • what is the " maximum number of positions"? are would just split the line values and take the Count from that?

  • I found the number of positions, I found this coddgo int numberPositions = dadosCSV.length .

  • yes it was that, the variable dadosCSV has the array of line contents obtained after the split, lenght shows exactly how many items are in the array :)

2 answers

2


This line in your code that makes the split, already have all the elements separated, so just use the property length to obtain the quantity:

String[] dadosCSV = linha.split(VIRGULA);
Int quantidade = dadosCSV.length;

0

Using the length method returns how many positions there are in an array.

dadosCSV.length

Browser other questions tagged

You are not signed in. Login or sign up in order to post.