删除数组中的元素

编程入门 行业动态 更新时间:2024-10-27 18:29:55
本文介绍了删除数组中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个由n个元素组成的数组.数字n是未知的.

I have an array consisting of n elements. The number n is not known. How to delete those n elements from the array?

推荐答案

从您的问题来看,您似乎需要从数组中删除所有元素.如果是这样,请将您的数组变量分配给一个新的空数组.我不确定这个简单的解决方案是否是您真正想要的.您需要更准确地回答问题. 如果您想要删除任何随机元素的能力,则数组还不够好.相反,您需要使用Vector,List,LinkedList或ArrayList并使用它们的remove方法. 请参阅 docs.oracle/javase/1.4. 2/docs/api/java/util/Collection.html [ ^ ];其他收集类型在此页面的另请参见"下列出;请点击每个链接,看看它们有什么用.
—SA
From your question, it looks like you need to delete all elements from the array. If so, assign your array variable to a new empty array. I am not sure that this trivial solution is what you really want. You need to be more accurate in questions. If you want an ability to remove any random element, arrays are not good enough. Instead, you need to use Vector, List, LinkedList or ArrayList and use their remove methods. Please see docs.oracle/javase/1.4.2/docs/api/java/util/Collection.html[^]; other collection types are listed on this page under "See Also"; please follow the links on each one to see what are they good for.
—SA

尝试一下: splice()方法向数组添加元素和/或从数组中删除元素,然后返回删除的元素. 语法: Try this : The splice() method adds and/or removes elements to/from an array, and returns the removed element(s). Syntax: array.splice(index,howmany,element1,.....,elementX)

index :必需.一个整数,指定要在什么位置添加/删除元素 howmany :必需.要删除的元素数.如果设置为0,则不会删除任何元素 element1,...,elementX :可选.要添加到数组中的新元素 Example1:

index : Required. An integer that specifies at what position to add/remove elements howmany : Required. The number of elements to be removed. If set to 0, no elements will be removed element1, ..., elementX : Optional. The new element(s) to be added to the array Example1 :

var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.write("Added: " + fruits.splice(2,0,"Lemon") + "<br />"); document.write(fruits);

Example2:

Example2 :

var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.write("Removed: " + fruits.splice(2,1,"Lemon") + "<br />"); document.write(fruits);

输出1:

Output1 :

Added: Banana,Orange,Lemon,Apple,Mango

Output2: 从位置2删除一个元素,然后向数组中的位置2添加一个新元素:

Output2 : Remove one element from position 2, and add a new element to position 2 in the array:

Removed: Apple Banana,Orange,Lemon,Mango

您在看第n​​个元素吗?然后您可以执行以下操作 Are you looking at the n''th element? Then you can do as below ArrayList a = new ArrayList(); a.Count

更多推荐

删除数组中的元素

本文发布于:2023-10-18 02:13:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1502783.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中   元素

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!