Java 如何从数组中删除一个项并向下移动其他所有项?

Java 如何从数组中删除一个项并向下移动其他所有项?,java,arrays,Java,Arrays,我有一个联系人对象数组,最多有50个联系人,但数量要少得多,因此该数组的初始化大小为50。但我需要我的方法来移除触点,并在它启动后移动所有东西。我所拥有的似乎有时有效,但并非每次都有效 public Contact remove(String lstnm) { int contactIndex = findContactIndex(lstnm); // Gets the index of the Contact that needs to be removed Con

我有一个联系人对象数组,最多有50个联系人,但数量要少得多,因此该数组的初始化大小为50。但我需要我的方法来移除触点,并在它启动后移动所有东西。我所拥有的似乎有时有效,但并非每次都有效

   public Contact remove(String lstnm)
  {
       int contactIndex = findContactIndex(lstnm); // Gets the index of the Contact that needs to be removed
  Contact contactToBeRemoved;

  if(contactIndex == -1) // If the Contact is not in the Array
  {
      contactToBeRemoved = null;
  }
  else
  {
      contactToBeRemoved = Contact_List[contactIndex]; // Assigns the Contact that is going to be removed
      for(int i = contactIndex; i < numContacts; i++) // From where the Contact was removed to the last Contact in the list
      {
          Contact_List[i] = Contact_List[i + 1]; // Shift all of the Contacts after the one removed down
      }
      numContacts -= 1; // One Contact is removed from the total number of Contacts
  }
  return contactToBeRemoved;
删除公共联系人(字符串lstnm)
{
int contactIndex=findContactIndex(lstnm);//获取需要删除的联系人的索引
接触触点被移除;
if(contactIndex=-1)//如果联系人不在数组中
{
contactToBeRemoved=null;
}
其他的
{
contactToBeRemoved=联系人列表[contactIndex];//指定要删除的联系人
for(int i=contactIndex;i

}

您的代码将在numContacts的第次迭代中出现异常,因为i+1将超出数组的大小

   for(int i = contactIndex; i < numContacts-1; i++) 
          {
              Contact_List[i] = Contact_List[i + 1]; 
          }
    Contact_List[Contact_List.length-1] = null;
for(int i=contactIndex;i

<强> PS:<强>这是一个非常糟糕的做法,在这种情况下使用数组,考虑使用ARARYLAMP。

<代码>数组< /代码>一个固定的大小,不能调整大小。另一方面,代码>数组列表会在每次添加元素时自动调整大小

因此,如果我有一个
数组
为5,我可以在其中放入5项,不多不少。您可以做的一件事是将
数组中的对象设置为
null
或0


编辑:关于您的评论,只需对
数组进行排序即可。在Java中查找一个简单的冒泡排序算法。

为什么不将数组转换为一个数组,并使用与您描述的完全相同的方法


这将为您节省一些时间和一些测试。

使用集合而不是数组,这样您就不必执行所有的移位过程! 集合会自动移动元素,您无需担心

你可以这样做

ArrayList<Contact> list=new ArrayList<Contact>();
Contact c=new Contact();

Contact.Add(Contact);
Contact.remove(Contact);
但是您必须重写Contact类中对象类的equal()和compareTo()方法! 否则什么都不能正常工作

用于此类用途

ArrayList
在内部维护一个数组,并在添加或从中删除元素时重新调整大小和结构


您也可以使用类似数据结构的
Vector
,但它是线程安全的。

在我看来,当您知道如何使用arrayList时,数组就变得非常无用了。我建议使用ArrayList。

创建ht econtact arrayList时,请执行以下操作:

import java.util.ArrayList;

public static void main(String args[]){
ArrayList<Contact> contacts = new ArrayList();
import java.util.ArrayList;
公共静态void main(字符串参数[]){
ArrayList联系人=新建ArrayList();
联系人。添加(新联系人()); }

使用ArrayList,这是最好的方法。阅读教程,其中有很多。 我建议它是因为arralist是动态的,这意味着您可以添加和删除项目,并且它为您调整了大小

希望我能帮忙,即使我的答案不是很完整

试试看

    System.arraycopy(contactList, contactIndex + 1, contactList, contactIndex, contactList.length - contactIndex - 1);

注意,Salay.ArayEnrimo是复制/移动数组元素

的最有效方法。也许你应该考虑使用java集合,例如ARAYListDebug——我还有30种其他方法都使用数组,所以现在我不能更改它。是的,但是数组的其余部分只是空引用。当一个被移除时,我希望它后面的所有内容都向下移动,所以所有实际的联系人都在数组的开头,其余的都是空的。
array.add(new Contact());
array.remove(contact); //assuming Contact class overrides equals()
import java.util.ArrayList;

public static void main(String args[]){
ArrayList<Contact> contacts = new ArrayList();
    System.arraycopy(contactList, contactIndex + 1, contactList, contactIndex, contactList.length - contactIndex - 1);