java實現無序數組結構

一、數組的2種定義方式

數據類型 []? 數組名稱 =?new?數據類型[數組長度];

這里 [] 可以放在數組名稱的前面,也可以放在數組名稱的后面,一般放在名稱的前面

數據類型 [] 數組名稱 = {數組元素1,數組元素2,......}

這種方式聲明數組的同時直接給定了數組的元素,數組的大小有給定的數組元素個數決定

public class ArrayStruct {public static void main(String[] args) {
//        int[] nums = new int[10];
//        int nums[] = new int[10];
//        nums = initArray( nums );//        int[] nums = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };int nums[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };printArray( nums );}public static int[] initArray( int[] arr ){for( int i = 0; i < arr.length; i++ ){arr[i] = i * 10;}return arr;}public static void printArray( int[] arr ){for( int i = 0; i < arr.length; i++ ){System.out.print( arr[i] + "\t" );}System.out.println();}
}

?二,實現一個自定義的數組結構,包含以下基本操作:

>插入數據

>刪除數據

>查找數據

>遍歷數據等

?

package com.ghostwu;class MyDefineArrayStruct {private int[] arr;private int curLen;private int length;public MyDefineArrayStruct(){curLen = 0;length = 30;arr = new int[length];}public MyDefineArrayStruct( int _length ) {curLen = 0;length = _length;arr = new int[length];}public int length (){return curLen;}public void print(){for( int i = 0; i < curLen; i++ ){System.out.print( arr[i] + "\t" );}System.out.println( );}public boolean add( int _value ){if( curLen >= length ){return false;}else{arr[curLen++] = _value;}return true;}public int getItem( int _index ){if( _index < 0 || _index > curLen ) {System.out.println( "數組下標越界" );}return arr[_index];}public int find( int _value ){int i;for( i = 0; i < curLen; i++ ){if( arr[i] == _value ){break;}}if( i == curLen ) {return -1;}return i;}public boolean delItem( int _value ){int res = find( _value );if( res == -1 ) return false;else {if( res == curLen - 1 ) {curLen--;}else {for( int i = res; i < curLen - 1; i++ ){arr[i] = arr[i+1];}curLen--;}            }return true;}public boolean updateItem( int _oldValue, int _newValue ){int res = find( _oldValue );if( res == -1 ){System.out.println( "數組中不存在" + _oldValue );return false;}else{arr[res] = _newValue;return true;}}}public class SelfDefineArrayStruct {public static void main(String[] args) {MyDefineArrayStruct arr = new MyDefineArrayStruct( 10 );arr.print();arr.add( 10 );arr.add( 20 );arr.add( 30 );arr.add( 40 );                arr.add( 100 );arr.print();arr.delItem( 10 );arr.print();System.out.println( arr.length() );arr.delItem( 20 );System.out.println( arr.length() );arr.updateItem( 30, 300 );arr.updateItem( 40, 400 );System.out.println( arr.length() );arr.print();}}

?

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

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

相關文章

Android App 的主角:Activity

Android App 程序主要由4種類型組成&#xff1a; 1.Activity&#xff08;活動&#xff09;&#xff1a;主要負責屏幕顯示畫面&#xff0c;并處理與用戶的互動。每個Android App至少都會有一個Activity&#xff0c;在程序一啟動時顯示主畫面供用戶操作。 2.Service&#xff08;后…

通過構建Paint App學習React Hooks

According to people in the know, React Hooks are hot, hot, hot. In this article, we follow Christian Jensens 14-part tutorial to find out about the basics of this new feature of React. Follow along to find out more! 據知情人士稱&#xff0c;React Hooks很熱&…

正則表達式 匹配常用手機號 (13、15\17\18開頭的十一位手機號)

原文:正則表達式 匹配常用手機號 &#xff08;13、15\17\18開頭的十一位手機號&#xff09;^1[3578]\d{9}$ ^1表示以1開頭&#xff0c;[3578]表示第二位的數字為3578中的任意一個&#xff0c;\d{9}表示0~9范圍內的數字匹配九次,$表示結束&#xff0c;12位以上的數字不匹配。

Npoi導出excel整理(附源碼)

前些日子做了一個簡單的winform程序&#xff0c;需要導出的功能&#xff0c;剛開始省事直接使用微軟的組件&#xff0c;但是導出之后發現效率極其低下&#xff0c;絕對像web那樣使用npoi組件&#xff0c;因此簡單的進行了整理&#xff0c;包括直接根據DataTable導出excel及Data…

44. 通配符匹配

44. 通配符匹配 給定一個字符串 (s) 和一個字符模式 &#xff0c;實現一個支持 ‘?’ 和 ‘*’ 的通配符匹配。 ? 可以匹配任何單個字符。 * 可以匹配任意字符串&#xff08;包括空字符串&#xff09;。 兩個字符串完全匹配才算匹配成功。說明: s 可能為空&#xff0c;且…

遞歸javascript_使用freeCodeCamp挑戰解釋了JavaScript中的遞歸

遞歸javascriptIn this article I will touch on a few important ideas to help you understand Recursion in JavaScript. I’m not going to give a full definition here, but you can take a look at what Wikipedia has to say. 在本文中&#xff0c;我將介紹一些重要的想…

入庫成本與目標成本對比報表中我學到的東西

1、SQL方面&#xff1a; &#xff08;1&#xff09;、用DECODE函數解決除數為零的情況 具體語法&#xff1a; DECODE&#xff08;參數&#xff0c;0&#xff0c;1&#xff0c;參數&#xff09; ->DECODE(TAB1.A8&#xff0c;0&#xff0c;1&#xff0c;TAB1.A8) &#xff08…

J - Borg Maze

J - Borg Maze 思路&#xff1a;bfs最小生成樹。#include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 110 using namespace std; int fa[MAXN]; struct nond{int x,y,z; }v[MAXN*MAXN]; s…

1095. 山脈數組中查找目標值

1095. 山脈數組中查找目標值 &#xff08;這是一個 交互式問題 &#xff09; 給你一個 山脈數組 mountainArr&#xff0c;請你返回能夠使得 mountainArr.get(index) 等于 target 最小 的下標 index 值。 如果不存在這樣的下標 index&#xff0c;就請返回 -1。 何為山脈數組…

【小摘抄】關于C++11下 string各類用法(持續更新)

http://blog.csdn.net/autocyz/article/details/42391155 提供了最簡單的詳解 下列對本人近期開發中的一些心得體會進行摘抄 1.string按照字符進行截取 示例代碼&#xff1a; string teststring "#12313#kajlkfdsa";//通訊消息示例&#xff0c;結合string的內置函數…

sql綜合練習題

一、表關系 年級表&#xff1a;class_grade create table class_grade(gid int primary key auto_increment,gname varchar(20) not null); insert into class_grade(gname) values(一年級),(二年級),(三年級); 班級表&#xff1a;class create table class(cid int primary ke…

javascript原型_在JavaScript中凍結原型時會發生什么

javascript原型Have you wondered what happens when you freeze the prototype of an object? Lets find out together.您是否想過凍結對象的原型時會發生什么&#xff1f; 讓我們一起找出答案。 對象 (Objects) In JavaScript, objects are dynamic collections of propert…

遲來的2017總結

明天就是年后第一天上班了&#xff08;過年期間請了6天假&#xff09;&#xff0c; 打算今天寫一下2017的總結&#xff0c;本來還想寫2018的愿景的&#xff0c;不過想想還是算了&#xff0c;現在沒什么想法&#xff0c;不想敷衍了事。 先貼一個2017的提升計劃&#xff1a; http…

tomcat啟動卡住

新部署的項目啟動tomcat后一直停在org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.16&#xff0c;卡在了org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/tomcat/web…

怎樣準備阿里技術面試_如何準備技術面試

怎樣準備阿里技術面試In June 2020 I watched an inspiring talk by Anthony D. Mays, a technical coach and founder at Morgan Latimerco. He came on a Facebook Developer Circles Benin live session and talked about how to prepare for a technical interview. 2020年…

通過一個簡單例子理解 RecyclerView.ItemDecoration

一、前言 RecyclerView 是從5.0推出的 MD 風格的控件。RecyclerView 之前有 ListView、GridView&#xff0c;但是功能很有限&#xff0c;例如 ListView 只能實現垂直方向上的滑動等。但是存在則合理&#xff0c;ListView 卻沒有被官方標記為 Deprecated&#xff0c;有興趣的同學…

Entity Framework Logging and Intercepting Database Operations (EF6 Onwards)

參考官方文檔&#xff1a;https://msdn.microsoft.com/en-us/library/dn469464(vvs.113).aspx轉載于:https://www.cnblogs.com/liandy0906/p/8473110.html

面試題 17.14. 最小K個數

面試題 17.14. 最小K個數 設計一個算法&#xff0c;找出數組中最小的k個數。以任意順序返回這k個數均可。 示例&#xff1a; 輸入&#xff1a; arr [1,3,5,7,2,4,6,8], k 4 輸出&#xff1a; [1,2,3,4] 提示&#xff1a; 0 < len(arr) < 1000000 < k < min(1…

這是您現在可以免費獲得的115張Coursera證書(在冠狀病毒大流行期間)

At the end of March, the world’s largest Massive Open Online Course provider Coursera announced that they are offering 100 free courses in response to the impact of the COVID-19 pandemic. 3月底&#xff0c;全球最大的大規模在線公開課程提供商Coursera 宣布 &a…

由淺入深理解----java反射技術

java反射機制詳解 java反射機制是在運行狀態下&#xff0c;對任意一個類可以獲取該類的屬性和方法&#xff0c;對任意一個對象可以調用其屬性和方法。這種動態的獲取信息和調用對象的方法的功能稱為java的反射機制 class<?>類&#xff0c;在java.lang包下面&#xff0c;…