稀疏数组联系:将数组保存至文件中再读取
1. /** 2. * 练习,1、将得到的稀疏数组保存到文件map.txt中 3. */ 4. FileWriter writer = null; 5. try { 6. File file = new File("E:\\IDEA Project\\DataStructures\\SparseArray\\map.txt"); 7. writer = new FileWriter(file); 8. for (int[] row : sparseArr) { 9. for (int data : row) { 10. writer.write(data + ","); 11. } 12. } 13. } catch (IOException e) { 14. e.printStackTrace(); 15. } finally { 16. if (writer != null) { 17. try { 18. writer.close(); 19. } catch (IOException e) { 20. e.printStackTrace(); 21. } 22. } 23. } 24. /** 25. * 练习:2、从文件中读取数据 26. */ 27. 28. File file = new File("E:\\IDEA Project\\DataStructures\\SparseArray\\map.txt"); 29. FileReader reader = new FileReader(file); 30. BufferedReader bufferedReader = new BufferedReader(reader); 31. String line = bufferedReader.readLine(); 32. String[] split = line.split(","); 33. int row = split.length / 3; 34. int[][] SparseArray1 = new int[row][3]; 35. for (int i = 0; i < split.length; i++) { 36. int parseInt = Integer.parseInt(split[i]); 37. SparseArray1[i / 3][i % 3] = parseInt; 38. } 39. //稀疏数组遍历: 40. System.out.println("输出新的稀疏数组:"); 41. for (int[] rw : SparseArray1) { 42. for (int data : rw) { 43. System.out.print(data + "\t"); 44. } 45. System.out.println(); 46. } 47. 48. bufferedReader.close(); 49. 50. 51. } 来自 <http://www.planetb.ca/projects/syntaxHighlighter/popup.php>
1. /** 2. * 练习,1、将得到的稀疏数组保存到文件map.txt中 3. */ 4. FileWriter writer = null; 5. try { 6. File file = new File("E:\\IDEA Project\\DataStructures\\SparseArray\\map.txt"); 7. writer = new FileWriter(file); 8. for (int[] row : sparseArr) { 9. for (int data : row) { 10. writer.write(data + ","); 11. } 12. } 13. } catch (IOException e) { 14. e.printStackTrace(); 15. } finally { 16. if (writer != null) { 17. try { 18. writer.close(); 19. } catch (IOException e) { 20. e.printStackTrace(); 21. } 22. } 23. } 24. /** 25. * 练习:2、从文件中读取数据 26. */ 27. 28. File file = new File("E:\\IDEA Project\\DataStructures\\SparseArray\\map.txt"); 29. FileReader reader = new FileReader(file); 30. BufferedReader bufferedReader = new BufferedReader(reader); 31. String line = bufferedReader.readLine(); 32. String[] split = line.split(","); 33. int row = split.length / 3; 34. int[][] SparseArray1 = new int[row][3]; 35. for (int i = 0; i < split.length; i++) { 36. int parseInt = Integer.parseInt(split[i]); 37. SparseArray1[i / 3][i % 3] = parseInt; 38. } 39. //稀疏数组遍历: 40. System.out.println("输出新的稀疏数组:"); 41. for (int[] rw : SparseArray1) { 42. for (int data : rw) { 43. System.out.print(data + "\t"); 44. } 45. System.out.println(); 46. } 47. 48. bufferedReader.close(); 49. 50. 51. }