首頁 Java Java基礎 JAVA如何實現數組的元素刪除?

JAVA如何實現數組的元素刪除?

Nov 12, 2019 pm 01:30 PM
java 刪除元素 陣列

JAVA如何實現數組的元素刪除?

JAVA如何實作陣列的元素刪除?

Java陣列的長度固定,因此無法直接刪除陣列中的元素。 (建議教學:java教學 )

透過建立新的數組,將保留的原始數組中的元素賦值到新數組來實現原始數組元素的刪除。同理,可以實現數組添加元素。

import java.util.Arrays;
 
public class ArrayTest1 {
 
	public static void main(String[] args) {
		int[] array1 = new int[] {4, 5, 6, 7};
		int num = 2;
		int[] newArray = new int[array1.length-1];
		
		for(int i=0;i<newArray.length; i++) {
			// 判断元素是否越界
			if (num < 0 || num >= array1.length) {
				throw new RuntimeException("元素越界... "); 
			} 
			// 
			if(i<num) {
				newArray[i] = array1[i];
			}
			else {
				newArray[i] = array1[i+1];
			}
		}
		// 打印输出数组内容
		System.out.println(Arrays.toString(array1));
		array1 = newArray;
		System.out.println(Arrays.toString(array1));
	}
}

以上是JAVA如何實現數組的元素刪除?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

PHP教程
1595
276
如何使用PHP中的陣列 如何使用PHP中的陣列 Aug 20, 2025 pm 07:01 PM

phparrayshandledatAcollectionsefefityIndexedorassociativuctures; hearecreatedWithArray()或[],訪問decessedviakeys,modifybyAssignment,iteratifybyAssign,iteratedwithforeach,andManipulationUsfunsionsFunctionsLikeCountLikeCountLikeCountLikeCountLikecount()

您目前尚未使用附上的顯示器[固定] 您目前尚未使用附上的顯示器[固定] Aug 19, 2025 am 12:12 AM

Ifyousee"YouarenotusingadisplayattachedtoanNVIDIAGPU,"ensureyourmonitorisconnectedtotheNVIDIAGPUport,configuredisplaysettingsinNVIDIAControlPanel,updatedriversusingDDUandcleaninstall,andsettheprimaryGPUtodiscreteinBIOS/UEFI.Restartaftereach

Java的僵局是什麼,您如何防止它? Java的僵局是什麼,您如何防止它? Aug 23, 2025 pm 12:55 PM

AdeadlockinJavaoccurswhentwoormorethreadsareblockedforever,eachwaitingforaresourceheldbytheother,typicallyduetocircularwaitcausedbyinconsistentlockordering;thiscanbepreventedbybreakingoneofthefournecessaryconditions—mutualexclusion,holdandwait,nopree

如何在Java中使用可選的? 如何在Java中使用可選的? Aug 22, 2025 am 10:27 AM

useoptional.empty(),可選of(),andoptional.ofnullable()

PS油漆濾清器灰色固定 PS油漆濾清器灰色固定 Aug 18, 2025 am 01:25 AM

TheOilPaintfilterinPhotoshopisgreyedoutusuallybecauseofincompatibledocumentmodeorlayertype;ensureyou'reusingPhotoshopCS6orlaterinthefulldesktopversion,confirmtheimageisin8-bitperchannelandRGBcolormodebycheckingImage>Mode,andmakesureapixel-basedlay

使用Micronaut構建雲原生爪哇應用 使用Micronaut構建雲原生爪哇應用 Aug 20, 2025 am 01:53 AM

Micronautisidealforbuildingcloud-nativeJavaapplicationsduetoitslowmemoryfootprint,faststartuptimes,andcompile-timedependencyinjection,makingitsuperiortotraditionalframeworkslikeSpringBootformicroservices,containers,andserverlessenvironments.1.Microna

修復:Windows顯示'客戶不持有所需的特權” 修復:Windows顯示'客戶不持有所需的特權” Aug 20, 2025 pm 12:02 PM

runtheapplicationorcommandasadministratorByright-clickingandSelecting“ runasAdministrator” toensureeleeleeleeleviledprivilegesareAreDranted.2.checkuseracccountcontontrol(uac)uac)

用於安全編碼的Java加密體系結構(JCA) 用於安全編碼的Java加密體系結構(JCA) Aug 23, 2025 pm 01:20 PM

理解JCA核心組件如MessageDigest、Cipher、KeyGenerator、SecureRandom、Signature、KeyStore等,它們通過提供者機制實現算法;2.使用SHA-256/SHA-512、AES(256位密鑰,GCM模式)、RSA(2048位以上)和SecureRandom等強算法與參數;3.避免硬編碼密鑰,使用KeyStore管理密鑰,並通過PBKDF2等安全派生密碼生成密鑰;4.禁用ECB模式,採用GCM等認證加密模式,每次加密使用唯一隨機IV,並及時清除敏

See all articles