Как получить доступ к массиву - Java

Узнай цену своей работы

Формулировка задачи:

Друзья, нужна помощь.
Листинг программы
  1. import javafx.event.ActionEvent;
  2. import javafx.fxml.FXML;
  3. import javafx.scene.control.Button;
  4. import javafx.scene.control.TextField;
  5. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  6. import org.apache.poi.ss.usermodel.Cell;
  7. import org.apache.poi.ss.usermodel.Row;
  8. import org.apache.poi.ss.usermodel.Sheet;
  9. import org.apache.poi.ss.usermodel.Workbook;
  10. import sample.objects.Parser;
  11. import java.io.FileInputStream;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14.  
  15. public class Controller {
  16.  
  17. private int ClusterNumber;
  18. private int ParameterNumber;
  19. @FXML
  20. private TextField clusterNTF;
  21. @FXML
  22. private TextField parameterNTF;
  23. @FXML
  24. private Button idGetData;
  25. public void onEnteredData(ActionEvent event) {
  26. if (!clusterNTF.getText().isEmpty()){
  27. ClusterNumber = Integer.parseInt(clusterNTF.getText());
  28. }
  29. if (!parameterNTF.getText().isEmpty()){
  30. ParameterNumber = Integer.parseInt(parameterNTF.getText());
  31. }
  32. }
  33.  
  34. public void onNewForm(ActionEvent actionEvent) throws IOException {
  35. System.out.println(ClusterNumber + ParameterNumber);
  36. Workbook wb_out = new HSSFWorkbook();
  37. Sheet sheet = wb_out.createSheet("Данные");
  38.  
  39. Row row1 = sheet.createRow(1);
  40. Cell cell_industry = row1.createCell(0);
  41. cell_industry.setCellValue("Промышленность");
  42. Row row0 = sheet.createRow(0);
  43. Cell cell_parameter = row0.createCell(0);
  44. cell_parameter.setCellValue("Параметр");
  45. for(int cell_i = 1; cell_i < ParameterNumber + 1; cell_i++ ) {
  46. Cell head = row0.createCell(cell_i);
  47. head.setCellValue(cell_i + "-параметр");
  48. }
  49. for(int row_i = 2; row_i < ClusterNumber + 2; row_i++){
  50. Row row = sheet.createRow(row_i);
  51. Cell cell_out = row.createCell(0);
  52. cell_out.setCellValue(row_i - 1 + " кластер");
  53. }
  54.  
  55. FileOutputStream fos = new FileOutputStream("Формула.xls");
  56. wb_out.write(fos);
  57. fos.close();
  58. wb_out.close();
  59.  
  60. }
  61. public void onGetData(ActionEvent actionEvent) throws IOException {
  62. System.out.println("Привет");
  63. FileInputStream fileIn = new FileInputStream("Формула.xls");
  64. Workbook wb = new HSSFWorkbook(fileIn);
  65. double[][] DataArray = new double[ParameterNumber][ClusterNumber];
  66.  
  67. Sheet sheet = wb.getSheetAt(0);
  68. for (int column_i = 1; column_i < ParameterNumber + 1; column_i++){
  69. for (int row_i = 2; row_i < ClusterNumber + 2; row_i++) {
  70. Row row = sheet.getRow(row_i);
  71. Cell cell_in = row.getCell(column_i);
  72. DataArray[column_i - 1][row_i - 2] = Double.parseDouble(Parser.getCellText(cell_in));
  73. }
  74. }
  75. fileIn.close();
  76. }
  77. public void onClusterNTF(ActionEvent event) {
  78. }
  79. public void onParameterNTF(ActionEvent event) {
  80. }
  81.  
  82. public void onCalculateData(ActionEvent event) {
  83. }
  84. }
Мне нужно получить доступ или как-то скопировать информацию из массива DataArray, доступ нужен здесь: onCalculateData. Спасибо за помощь)

Решение задачи: «Как получить доступ к массиву»

textual
Листинг программы
  1. import javafx.event.ActionEvent;
  2. import javafx.fxml.FXML;
  3. import javafx.scene.control.Button;
  4. import javafx.scene.control.TextField;
  5. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  6. import org.apache.poi.ss.usermodel.Cell;
  7. import org.apache.poi.ss.usermodel.Row;
  8. import org.apache.poi.ss.usermodel.Sheet;
  9. import org.apache.poi.ss.usermodel.Workbook;
  10. import sample.objects.Parser;
  11. import java.io.FileInputStream;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14.  
  15.  
  16. public class Controller {
  17.  
  18.  
  19.     private int ClusterNumber;
  20.     private int ParameterNumber;
  21.    
  22.     @FXML
  23.     private TextField clusterNTF;
  24.  
  25.     @FXML
  26.     private TextField parameterNTF;
  27.  
  28.     @FXML
  29.     private Button idGetData;
  30.     private double[][] DataArray;
  31.     public void onEnteredData(ActionEvent event) {
  32.  
  33.         if (!clusterNTF.getText().isEmpty()){
  34.             ClusterNumber = Integer.parseInt(clusterNTF.getText());
  35.         }
  36.         if (!parameterNTF.getText().isEmpty()){
  37.             ParameterNumber = Integer.parseInt(parameterNTF.getText());
  38.         }
  39.     }
  40.  
  41.  
  42.  
  43.     public void onNewForm(ActionEvent actionEvent) throws IOException {
  44.         System.out.println(ClusterNumber + ParameterNumber);
  45.  
  46.         Workbook wb_out = new HSSFWorkbook();
  47.         Sheet sheet = wb_out.createSheet("Данные");
  48.  
  49.  
  50.         Row row1 = sheet.createRow(1);
  51.         Cell cell_industry = row1.createCell(0);
  52.         cell_industry.setCellValue("Промышленность");
  53.  
  54.         Row row0 = sheet.createRow(0);
  55.         Cell cell_parameter = row0.createCell(0);
  56.         cell_parameter.setCellValue("Параметр");
  57.         for(int cell_i = 1; cell_i < ParameterNumber + 1; cell_i++ ) {
  58.             Cell head = row0.createCell(cell_i);
  59.             head.setCellValue(cell_i + "-параметр");
  60.         }
  61.  
  62.         for(int row_i = 2; row_i < ClusterNumber + 2; row_i++){
  63.             Row row = sheet.createRow(row_i);
  64.             Cell cell_out = row.createCell(0);
  65.             cell_out.setCellValue(row_i - 1 + " кластер");
  66.         }
  67.  
  68.  
  69.         FileOutputStream fos = new FileOutputStream("Формула.xls");
  70.         wb_out.write(fos);
  71.         fos.close();
  72.         wb_out.close();
  73.  
  74.  
  75.     }
  76.  
  77.     public void onGetData(ActionEvent actionEvent) throws IOException {
  78.  
  79.         System.out.println("Привет");
  80.  
  81.         FileInputStream fileIn = new FileInputStream("Формула.xls");
  82.         Workbook wb = new HSSFWorkbook(fileIn);
  83.         DataArray = new double[ParameterNumber][ClusterNumber];
  84.  
  85.  
  86.         Sheet sheet = wb.getSheetAt(0);
  87.         for (int column_i = 1; column_i < ParameterNumber + 1; column_i++){
  88.             for (int row_i = 2; row_i < ClusterNumber + 2; row_i++) {
  89.                 Row row = sheet.getRow(row_i);
  90.                 Cell cell_in = row.getCell(column_i);
  91.                 DataArray[column_i - 1][row_i - 2] = Double.parseDouble(Parser.getCellText(cell_in));
  92.             }
  93.         }
  94.  
  95.         fileIn.close();
  96.     }
  97.  
  98.     public void onClusterNTF(ActionEvent event) {
  99.     }
  100.  
  101.     public void onParameterNTF(ActionEvent event) {
  102.     }
  103.  
  104.  
  105.     public void onCalculateData(ActionEvent event) {       
  106.  
  107.     }
  108. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

9   голосов , оценка 4.222 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут