Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Feb 2006
    Messaggi
    258

    Map.size() cosa restituisce di preciso?

    Salve ragazzi,
    ho un grosso problema che non riesco a risolvere.Sto realizzando un'applicazione Java che permetta di memorizzare in ingresso un file.txt composto da una prima colonna di tipo stringa ed un numero variabile di rimanenti colonne di tipo double.
    L'obiettivo del mio programma è calcolare il numero di righe e colonne del file.
    La mia domanda è: le colonne saranno sempre 2?(Cioè quella di tipo stringa e l'array verrà considerato come una colonna?)Io ho usato il comando Map.size() però dal listato di codice che vi mostro dovrebbe uscirmi

    numero di colonne numeriche=3
    numero di colonne=4(c'è in più quella stringa)

    Invece mi esce

    numero di colonne numeriche=3
    numero di colonne=2(c'è in più quella stringa)

    Cioè quando calcolo Map.size(); nel mio caso mi deve dare 2(una stringa e un array) o un numero pari a 1 stringa+ x elementi dell'array?(come io mi aspettavo dall'output?)
    Grazie in anticipo

  2. #2
    Utente di HTML.it
    Registrato dal
    Feb 2006
    Messaggi
    258
    vi posto il codice,magari mi capite meglio....

    //package gbattine;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Map;
    import java.util.StringTokenizer;
    import java.util.HashMap;
    public class AddDb2 {
    private static String fileName = "Dato2.txt";
    private Map <String, double[]>dataMap = null;
    private int Nrows=0;
    private int Ncol=0;
    public static void main(String[] args) throws IOException {
    new AddDb2();
    }




    public AddDb2() throws IOException { //constructor
    /* Get a reference to the input file. */
    FileReader file = new FileReader(fileName);
    BufferedReader br = new BufferedReader(file);
    /* Create the empty map. */
    this.dataMap = new HashMap<String, double[]>();/*Constructs an empty HashMap
    with the default initial capacity (16) and the default load factor (0.75).*/
    /* Populate the map from the input file. */
    populateMap(br);
    /*int Dim=(dataMap.size());//include the first string row
    System.out.println("Il size del datamap e'"+Dim);*/
    Ncol=(numberOfNumericColumns+1);
    System.out.println("Il numero di colonne e'"+(Ncol));
    //System.out.println("Il numero di colonne del file è"+dataMap.size());
    /* Display the contents of the map. */
    displayMap(this.dataMap);
    /* Close the reader. */
    br.close();
    }



    /**
    * Populate an HashMap from an input file.
    *
    *

    This method accomodates an input file with any number of lines in
    it. It also accomodates any number of doubles on the input line as long as the
    first value on the line is a String. The number of doubles on each input line can also
    vary.</p>
    *
    * @param bufferedReader
    * @return a Map that has one element for each line of the input file;
    the key is the first column from the input file and the entry is the array
    of doubles that follows the first column
    * @throws IOException
    */
    public Map populateMap(BufferedReader bufferedReader) throws IOException
    {
    System.out.println("Caricamento dell'array di double in corso.....");
    /* Store each line of the input file as a separate key and entry in
    the Map. */
    String line = null;
    while ((line = bufferedReader.readLine()) != null) {
    //line = line.replace (',', '.');
    Nrows++;
    /* Create a tokenizer for the line. */
    StringTokenizer st = new StringTokenizer(line);
    /*
    * Assuming that the first column of every row is a String and
    the remaining columns are numbers, count the number of numeric columns.
    */
    //int ColNumber=st.countTokens();
    // int numberOfNumericColumns = ColNumber- 1;
    int numberOfNumericColumns = (st.countTokens()-1);
    //System.out.println("Il numero di colonne del file è"+dataMap.size());
    System.out.println("Il numero di colonne numeriche del file e'"+numberOfNumericColumns);
    /*
    * Get the first token from the line. It will be a String and
    its value will be a unique key for the rest of the row.
    */
    String key = st.nextToken().trim();
    /* Create the array for the numbers which make up the rest of the line. */
    double[] array = new double[numberOfNumericColumns];
    /* Populate the array by parsing the rest of the line. */
    for (int column = 0; column < numberOfNumericColumns; column++){
    array[column] = Double.parseDouble(st.nextToken().trim());
    }
    /* Store the first column as the key and the array as the entry.
    */
    this.dataMap.put(key, array); /*Associates the specified value with
    the specified key in this map.*/

    }
    System.out.println("Il numero di righe del file e'"+Nrows);
    //System.out.println("Il numero di colonne del file è"+dataMap.size());
    return this.dataMap;
    }












    public void displayMap(Map<String,double[]> myMap) {
    /* Iterate through the values of the map, displaying each key and
    its corresponding array. */
    for (Map.Entry<String, double[]> entry : myMap.entrySet()) {
    /* Get and display the key. */
    System.out.print(entry.getKey() + " : ");
    /* Get the array. */
    double[] myArray = entry.getValue();
    /*
    * Display each value in the array. Put a semicolon after each
    value except the last.
    * Keep all the values for a given key on a single line.
    */
    for (int ix = 0; ix < myArray.length; ix++) {
    if (ix < myArray.length - 1) { //the value is not the last one in the array ,allora metti ;
    System.out.print(myArray[ix] + "; ");
    } else { //the value is the last one in the array ,la linea finisce così
    System.out.println(myArray[ix]);
    }
    }
    }
    }
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.