稀疏數組Java練習代碼含IO

是在學習尚硅谷的數據結構與算法Java課程,課后自己憑借思路寫的練習代碼
首先定義一個稀疏數組類

import java.io.*;
import java.util.Objects;public class SparseArray {int sum;//創建原始數組public int[][] createArray(int column, int row){//根據傳入數據定義原始數組大小int[][] array = new int[row][column];//設置一些初始值int position1_row = 1;int position1_column = 2;int position1_value = 1;int position2_row = 3;int position2_column = 4;int position2_value = 2;//賦值初始值,默認值為0, 1表示數據1,2表示數據2System.out.println("生成初始數組如下:");for (int i = 0; i < array.length; i++) {for (int j = 0; j < column; j++) {if (i == position1_row && j == position1_column){array[i][j] = position1_value;}else if (i == position2_row && j == position2_column){array[i][j] = position2_value;}else {array[i][j] = 0;}System.out.printf("%d\t",array[i][j]);}System.out.println();}return array;}//根據原始數組創建稀疏數組public int[][] builtSparseArray(int[][] originalArray){int row = originalArray.length;int column = originalArray[0].length;int sum = 0;for (int i = 0; i < row; i++) {for (int data : originalArray[i]) {if (data != 0){sum++;}}}int[][] sparseArray = new int[sum+1][3];sparseArray[0][0] = row;sparseArray[0][1] = column;sparseArray[0][2] = sum;int count = 1;System.out.println("生成稀疏數組如下");System.out.printf("%d\t%d\t%d\t\n",row,column,sum);for (int i = 0; i < row; i++) {for (int j = 0; j < column; j++) {if (originalArray[i][j] != 0){sparseArray[count][0] = i;sparseArray[count][1] = j;sparseArray[count][2] = originalArray[i][j];System.out.printf("%d\t%d\t%d\t\n",i,j,originalArray[i][j]);count++;}}}return sparseArray;}//根據稀疏數組恢復原始數組public int[][] restoreArray(int[][] sparseArray){int[][] restoredArray = new int[sparseArray[0][0]][sparseArray[0][1]];for (int i = 0; i < sparseArray[0][0]; i++) {for (int j = 0; j < sparseArray[0][1]; j++) {restoredArray[i][j] = 0;}}for(int i = 1; i < sparseArray.length; i++){restoredArray[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];}System.out.println("復原數組如下:");for (int i = 0; i < sparseArray[0][0]; i++) {for (int j = 0; j < sparseArray[0][1]; j++) {System.out.printf("%d\t",restoredArray[i][j]);}System.out.println();}return restoredArray;}public void saveArray(int[][] array) {BufferedWriter writer = null;try {writer = new BufferedWriter(new FileWriter("sparseArray.txt"));for (int i = 0; i < array.length; i++) {for (int j = 0; j < array[0].length; j++) {int data = array[i][j];writer.write(data+",");}writer.newLine();}writer.close();System.out.println("稀疏數組保存成功");} catch (IOException e) {e.printStackTrace();}}public int[][] readArray(){int [][] sparseArray = null;try {String data;BufferedReader reader = new BufferedReader(new FileReader("sparseArray.txt"));boolean flag = true;int count = 0;System.out.println("讀到的稀疏數組如下:");while ((data = reader.readLine()) != null){String[] splits = data.split(",");if (flag){sum = Integer.parseInt(splits[2]);sparseArray = new int[sum+1][3];}flag = false;sparseArray[count][0] = Integer.parseInt(splits[0]);sparseArray[count][1] = Integer.parseInt(splits[1]);sparseArray[count][2] = Integer.parseInt(splits[2]);System.out.printf("%d\t%d\t%d\t\n",sparseArray[count][0],sparseArray[count][1],sparseArray[count][2]);count++;}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return (Objects.isNull(sparseArray))?null:sparseArray;}
}

然后主類代碼


public class Start {public static void main(String[] args) {//定義稀疏數組類對象SparseArray sparseArray = new SparseArray();//創建原始數組int[][] array = sparseArray.createArray(11, 11);//創建稀疏數組int[][] sparseArrayInstance = sparseArray.builtSparseArray(array);//保存稀疏數組sparseArray.saveArray(sparseArrayInstance);//讀取稀疏數組int[][] ints = sparseArray.readArray();//恢復原始數組sparseArray.restoreArray(sparseArrayInstance);}
}

打印結果,存儲是存到txt文件中

生成初始數組如下:
0	0	0	0	0	0	0	0	0	0	0	
0	0	1	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	2	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
生成稀疏數組如下
11	11	2	
1	2	1	
3	4	2	
稀疏數組保存成功
讀到的稀疏數組如下:
11	11	2	
1	2	1	
3	4	2	
復原數組如下:
0	0	0	0	0	0	0	0	0	0	0	
0	0	1	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	2	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	Process finished with exit code 0

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/38631.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/38631.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/38631.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

雨量氣象站:野外監測的得力助手

在廣闊無垠的大自然中&#xff0c;雨量、風力、風向、溫濕度以及大氣壓力等氣象數據對于各種應用場景都至關重要。特別是在野外、森林防火、山洪監測等無市電供電的場合&#xff0c;一款高效可靠的監測設備更是不可或缺。雨量氣象站正是為了滿足這些需求而誕生的戶外專用監測站…

C++ STL unique_ptr智能指針源碼剖析

由于上一篇博客將shared_ptr,weak_ptr,enable_shared_form_this的源碼實現整理了一遍,想著cpp智能指針還差個unique_ptr故寫下此篇博客,以供學習 源碼剖析 一,模板參數 首先,我們先看unique_ptr的模板參數,第一個參數_TP自是不用說表示對象類型,第二個模板參數定義了unique_p…

FFmpeg視頻處理工具安裝使用

一、前言 FFmpeg是流行的開源視頻處理工具&#xff0c;用于轉碼、合并、編輯等。以下是安裝和使用方法&#xff1a; 二、步驟 1.下載 1.1 ffmpeg下載 官網下載地址 wget https://www.ffmpeg.org/releases/ffmpeg-6.1.1.tar.xz1.2 nasm下載 https://www.nasm.us/pub/nasm/…

Android應用安裝過程

Android 系統源碼源碼-應用安裝過程 Android 中應用安裝的過程就是解析 AndroidManifest.xml 的過程&#xff0c;系統可以從 Manifest 中得到應用程序的相關信息&#xff0c;比如 Activity、Service、Broadcast Receiver 和 ContentProvider 等。這些工作都是由 PackageManage…

drm core

drm core初始化 /*** drm_sysfs_init - initialize sysfs helpers** This is used to create the DRM class, which is the implicit parent of any* other top-level DRM sysfs objects.** You must call drm_sysfs_destroy() to release the allocated resources.** Return: …

Linux通配符及其在文件搜索和管理中的應用

Linux通配符及其在文件搜索和管理中的應用 大家好&#xff0c;我是免費搭建查券返利機器人省錢賺傭金就用微賺淘客系統3.0的小編&#xff0c;也是冬天不穿秋褲&#xff0c;天冷也要風度的程序猿&#xff01; 1. 了解Linux通配符 在Linux系統中&#xff0c;通配符是一種用于匹…

家政小程序的開發,帶動市場快速發展,提高家政服務質量

當下生活水平逐漸提高&#xff0c;也增加了年輕人的工作壓力&#xff0c;同時老齡化也在日益增加&#xff0c;使得大眾對家政的需求日益提高&#xff0c;能力、服務質量高的家政人員能夠有效提高大眾的生活幸福指數。 但是&#xff0c;傳統的家政服務模式存在著效率低、用戶與…

慧翰股份毛利率下滑:股權轉讓糾紛引關注,研發費用率遠弱同行還買樓?

《港灣商業觀察》施子夫 6月11日&#xff0c;慧翰微電子股份有限公司&#xff08;以下簡稱&#xff0c;慧翰股份&#xff09;IPO注冊申請獲證監會同意&#xff0c;預計公司將很快登陸深交所創業板&#xff0c;保薦機構為廣發證券。 從業績面來看&#xff0c;過去三年&#xf…

基于X86+FPGA+AI的芯片缺陷檢測方案

應用場景 隨著半導體技術的發展&#xff0c;對芯片的良率要求越來越高。然而集成電路芯片制造工藝復雜&#xff0c;其制造過程中往往產生很多缺陷&#xff0c;因此缺陷檢測是集成電路制造過程中的必備工藝。 客戶需求 小體積&#xff0c;低功耗 2 x USB,1 x LAN Core-i平臺無…

JavaScript——運算符的優先級和結合性

目錄 任務描述 相關知識 運算符的優先級 運算符的結合性 編程要求 任務描述 本關任務&#xff1a;我們將給出函數mainJs()的完整代碼&#xff0c;要求在函數體內第三句以及第五句中添加適當的括號&#xff0c;實現編程要求里面的要求。 要想完成本關任務&#xff0c;必須…

一點連接千家銀行,YonSuite讓“銀企對賬”一鍵確認

在當今數智化浪潮下&#xff0c;成長型企業面臨著前所未有的機遇與挑戰。特別是在與銀行的對接以及銀企對賬等方面&#xff0c;傳統的手動操作模式已難以滿足企業高效、安全的金融管理需求。用友YonSuite作為一款全場景SaaS應用服務&#xff0c;憑借其強大的銀企直聯功能&#…

AI在線免費視頻工具3:聲音生視頻

1、聲音生視頻 Noisee&#xff1a;通過聲音生成對應視頻&#xff0c;可以增加prompt指定生成內容相關視頻 https://noisee.ai/create

【基礎篇】第5章 Elasticsearch 數據聚合與分析

在Elasticsearch的龐大功能體系中&#xff0c;數據聚合與分析扮演著至關重要的角色&#xff0c;它使我們能夠從海量數據中提煉出有價值的信息&#xff0c;為決策提供依據。本章將深入探討Elasticsearch的聚合功能&#xff0c;從基本概念到常見類型的實踐&#xff0c;讓你掌握如…

Elasticsearch 使用誤區之二——頻繁更新文檔

在使用 Elasticsearch 時&#xff0c;頻繁更新文檔是一種常見誤區。這不僅影響性能&#xff0c;還可能導致系統資源的浪費。 理解 Elasticsearch 的文檔更新機制對于優化性能至關重要。 關于 Elasticsearch 更新操作&#xff0c;常見問題如下&#xff1a; ——https://t.zsxq.c…

Spring Cloud實戰:構建分布式系統解決方案

Spring Cloud實戰&#xff1a;構建分布式系統解決方案 大家好&#xff0c;我是免費搭建查券返利機器人省錢賺傭金就用微賺淘客系統3.0的小編&#xff0c;也是冬天不穿秋褲&#xff0c;天冷也要風度的程序猿&#xff01;今天我們將深入探討如何使用Spring Cloud來構建分布式系統…

剖析DeFi交易產品之UniswapV4:概述篇

本文首發于公眾號&#xff1a;Keegan小鋼 UniswapV4 與 UniswapV3 相比&#xff0c;算法上并沒有什么改變&#xff0c;依然還是采用集中流動性模型&#xff0c;但架構上變化很大&#xff0c;包括功能架構&#xff0c;也包括技術架構。相比之前的版本&#xff0c;UniswapV4 最大…

百元藍牙耳機推薦2024,百元藍牙耳機排行榜盤點

在2024年面對琳瑯滿目的藍牙耳機選項&#xff0c;消費者往往難以抉擇&#xff0c;特別是在預算有限的情況下&#xff0c;如何在眾多產品中挑選出既滿足質量又符合預算的耳機成為了一個不小的挑戰。 為了幫助大家在繁多的選擇中找到真正物有所值的百元藍牙耳機&#xff0c;我們…

UnityUGUI之一:image和Rawimage

image組件的相關屬性 其中SpriteMode&#xff0c;若為單個圖片則為Single&#xff0c;圖片集則為Multiple 圖集的切割 點擊Slice可以進行自動切割 為且每個格子都可以進行單獨的九宮格切割 當圖片被九宮格切割再進行拉伸以后&#xff0c;九宮格的四角不會被拉伸 Tiled&#x…

構建支持多平臺的返利App跨平臺開發策略

構建支持多平臺的返利App跨平臺開發策略 大家好&#xff0c;我是免費搭建查券返利機器人省錢賺傭金就用微賺淘客系統3.0的小編&#xff0c;也是冬天不穿秋褲&#xff0c;天冷也要風度的程序猿&#xff01;今天我們將討論如何構建支持多平臺的返利App&#xff0c;特別關注跨平臺…

一棵B+樹可以存放多少行數據

以MySQL InnoDB為例。InnoDB存儲引擎最小儲存單元是頁&#xff0c;一頁大小固定是16KB&#xff0c;使用該引擎的表為索引組織表。B樹葉子存的是數據&#xff0c;內部節點存的是鍵值和指針。索引組織表通過非葉子節點的二分查找法以及指針確定數據在哪個頁中&#xff0c;進而再去…