Java 在ArrayList中添加和删除元素 我为添加和删除arraylist中的元素编写了这段代码。但我无法完成此代码的删除方法。 有人能帮我实现在这段代码中创建的remove方法吗? 包Comp10152\u链接列表; /** * *@作者普密基尚 */ 公共类SimpleRayList{ //默认容量的常量和 //调整大小的因素。 专用最终int默认_容量=10; 私有最终整数调整_系数=2; //私人领域 私有字符串[]列表;//列表 私有int元素;//存储的元素数 /**此构造函数创建一个空的 默认容量。 */ 公共SimpleArrayList() { 列表=新字符串[默认容量]; 元素=0; } /**在列表末尾添加一个字符串。 @param str要添加的字符串。 */ 公共空添加(字符串str) { //完成 //提示-一旦您知道放置元素的正确位置,请使用 //private add方法将项目放置在正确的位置 // } /**在特定索引处添加字符串。 @param索引添加的字符串的位置。 @param str要添加的字符串。 @索引时异常IndexOutboundsException 这是不允许的。 */ 公共void添加(int索引,String str) { //首先确保索引是有效的。 如果(索引>元素| |索引索引;index2--) 列表[index2]=列表[index2-1]; //在索引处添加新元素。 列表[索引]=str; //调整元素的数量。 元素++; } /**在列表中搜索指定的字符串。 @param str要搜索的字符串。 @如果列表包含字符串,则返回true; 否则就错了。 */ 公共布尔包含(字符串str) { int index=0;//索引计数器 布尔值find=false;//搜索标志 //单步浏览列表。当字符串 //已找到,将“找到”设置为true并停止。 while(!found&&index=元素| |索引

Java 在ArrayList中添加和删除元素 我为添加和删除arraylist中的元素编写了这段代码。但我无法完成此代码的删除方法。 有人能帮我实现在这段代码中创建的remove方法吗? 包Comp10152\u链接列表; /** * *@作者普密基尚 */ 公共类SimpleRayList{ //默认容量的常量和 //调整大小的因素。 专用最终int默认_容量=10; 私有最终整数调整_系数=2; //私人领域 私有字符串[]列表;//列表 私有int元素;//存储的元素数 /**此构造函数创建一个空的 默认容量。 */ 公共SimpleArrayList() { 列表=新字符串[默认容量]; 元素=0; } /**在列表末尾添加一个字符串。 @param str要添加的字符串。 */ 公共空添加(字符串str) { //完成 //提示-一旦您知道放置元素的正确位置,请使用 //private add方法将项目放置在正确的位置 // } /**在特定索引处添加字符串。 @param索引添加的字符串的位置。 @param str要添加的字符串。 @索引时异常IndexOutboundsException 这是不允许的。 */ 公共void添加(int索引,String str) { //首先确保索引是有效的。 如果(索引>元素| |索引索引;index2--) 列表[index2]=列表[index2-1]; //在索引处添加新元素。 列表[索引]=str; //调整元素的数量。 元素++; } /**在列表中搜索指定的字符串。 @param str要搜索的字符串。 @如果列表包含字符串,则返回true; 否则就错了。 */ 公共布尔包含(字符串str) { int index=0;//索引计数器 布尔值find=false;//搜索标志 //单步浏览列表。当字符串 //已找到,将“找到”设置为true并停止。 while(!found&&index=元素| |索引,java,arraylist,Java,Arraylist,我在下面的主类中调用上面的Simplearraylist类方法。我不知道SimpleArrayList类中remove方法的实现。谁能给我一个解决办法?最重要的是我不想在主课上有任何改变。我只想根据remove方法在主类中的调用来实现它的代码。 /* *要更改此许可证标题,请在“项目属性”中选择“许可证标题”。 *要更改此模板文件,请选择工具|模板 *然后在编辑器中打开模板。 */ package Comp10152\u链接列表; /** * *@作者普密基尚 */ 导入java.util.Ra

我在下面的主类中调用上面的Simplearraylist类方法。我不知道SimpleArrayList类中remove方法的实现。谁能给我一个解决办法?最重要的是我不想在主课上有任何改变。我只想根据remove方法在主类中的调用来实现它的代码。 /* *要更改此许可证标题,请在“项目属性”中选择“许可证标题”。 *要更改此模板文件,请选择工具|模板 *然后在编辑器中打开模板。 */

package Comp10152\u链接列表;
/**
*
*@作者普密基尚
*/
导入java.util.Random;
公共类COM10152_Lab4
{
公共静态void main(字符串[]args)
{
迭代的最终整数=10;
字符串名[]={“Amy”、“Bob”、“Al”、“Beth”、“Carol”、“Zed”、“Aaron”};
SimpleLink列表ll=新的SimpleLink列表(
I done this code for Add and Remove the elements from the arraylist. But I can't complete the remove method of this code. 

Can anyone help me for implement the remove method which is created in this code?


    package Comp10152_linkedlist;

    /**
     *
     * @author Bhumi-Kishan
     */
    public class SimpleArrayList {
      // Constants for the default capacity and
       // the resizing factor.
       private final int DEFAULT_CAPACITY = 10;
       private final int RESIZE_FACTOR = 2;

       // Private Fields
       private String[] list;  // The list
       private int elements;   // Number of elements stored

       /** This constructor creates an empty list of the
           default capacity.
       */
       public SimpleArrayList()
       {
          list = new String[DEFAULT_CAPACITY];
          elements = 0;
       }

       /** Add a string to the end of the list.
           @param str The string to add. 
       */

       public void add(String str)
       {
           // To be completedint
           // Hint - once you know the correct spot to place the elemnt use the 
           // private add method to place the item in the correct place
           //
       }

       /** Add a string at a specific index.
           @param index The added string's position.
           @param str The string to add.
           @exception IndexOutOtBoundsException When index
                      is out of bounds.
       */   
       public void add(int index, String str)
       {
          // First make sure the index is valid.
          if (index > elements || index < 0)
             throw new IndexOutOfBoundsException();

          // If the list is full, resize it.
          if (elements == list.length)
             resize();

          // Shift the elements starting at index
          // to the right one position.
          for (int index2 = elements; index2 > index; index2--)
             list[index2] = list[index2 - 1];

          // Add the new element at index.
          list[index] = str;

          // Adjust the number of elements.
          elements++;
       }

       /** Search the list for a specified string.
           @param str The string to search for.
           @return true if the list contains the string;
                   false otherwise.
       */
       public boolean contains(String str)
       {
          int index = 0;          // Index counter
          boolean found = false;  // Search flag

          // Step through the list. When the string
          // is found, set found to true and stop.
          while (!found && index < elements)
          {
             if (list[index].equals(str))
                found = true;
             index++;
          }

          // Return the status of the search.
          return found;
       }

       /** Get an element at a specific position.
           @param index The specified index.
           @return The element at index.
           @exception IndexOutOtBoundsException When index
                      is out of bounds.
       */
       public String get(int index)
       {
          if (index >= elements || index < 0)
             throw new IndexOutOfBoundsException();
          return list[index];
       }

       /** Determines whether the list is empty.
           @return true if the list is empty; false otherwise.
       */
       public boolean isEmpty()
       {
          return (elements == 0);
       }

       /** Remove a specific string from the list.
           @param str The string to remove.
           @return true if the string was found; false otherwise.
       */
       public boolean remove(String str)
       {
          // Hint : use text examples to assist or refer to the private add method above
          return false;


       }

       /** Get the number of elements in the list.
           @return The number of elements in the list.
       */
       public int size()
       {
          return elements;
       }

       /** Resizes the list to twice its current length. */
       private void resize()
       {
          // Calculate the new length, which is the current
          // length multiplied by the resizing factor.
          int newLength = list.length * RESIZE_FACTOR;

          // Create a new list.
          String[] tempList = new String[newLength];

          // Copy the existing elements to the new list.
          for (int index = 0; index < elements; index++)
             tempList[index] = list[index];

          // Replace the existing list with the new one.
          list = tempList;
       }

       /** Convert the list to a String
           @return A String with the same elements as the list.
       */
       public String toString()
       {
        StringBuilder strBuilder = new StringBuilder();

        // Use p to walk down the linked list
          // Store the elements in the array.
          for (int index = 0; index < elements; index++)
            strBuilder.append("[" + list[index] + "]");

          // Return the String.
          return strBuilder.toString();
       }  
    }
package Comp10152_linkedlist;

/**
 *
 * @author Bhumi-Kishan
 */

import java.util.Random;

public class Comp10152_Lab4 
{
   public static void main(String[] args)
  {
    final int NUMBER_OF_ITERATIONS = 10;
    String names[] = {"Amy", "Bob", "Al", "Beth", "Carol", "Zed", "Aaron"};
    SimpleLinkedList ll = new SimpleLinkedList();
    final int TOTALOPERATIONS = names.length * NUMBER_OF_ITERATIONS;

    Random random = new Random();

    for (int i=0; i<NUMBER_OF_ITERATIONS;i++)
    {
      for (int j=0; j<names.length; j++)
        ll.add(names[j]);
    }
     System.out.println("The members of list are:");
      System.out.println(ll);
    // remove half of the items in the list by selecting randomly from names
    for (int i=0; i<TOTALOPERATIONS/2;i++)
    {
      ll.remove(names[random.nextInt(names.length)]);
    }
    System.out.println("The members of list are:");
      System.out.println(ll);
    SimpleArrayList al = new SimpleArrayList();
    for (int i=0; i<NUMBER_OF_ITERATIONS;i++)
    {
      for (int j=0;j<names.length;j++)
         al.add(i,names[j]);
    }
      System.out.println("The members of array are:");
      System.out.println(al);

    // remove half of the items in the list by selecting randomly from names
    for (int i=0; i<TOTALOPERATIONS/2;i++)
    {
      al.remove(names[random.nextInt(names.length)]);
    }   
     System.out.println("The members of array are:");
      System.out.println(al);
  }     
}
public boolean remove(String str)
   {
      // Hint : use text examples to assist or refer to the private add method above

            for ( int i = 0;  i < list.size(); i++){
            String tempName = list.get(i);
            if(tempName.equals(str){
                name.remove(i);
               return true;
            }
          return false;
        }    
   }
public boolean remove(String str)  {

    for (int i=0;i<list.length;i++) {
        final String s = list[i];
        if (str == s || s.contentEquals(str)) {
            String[] newList = new String[list.length];
            System.arraycopy(list, 0, newList ,0, i);
            if (list.length > i+1) {
                System.arraycopy(list, i+1, newList, i, list.length - 1 - i);
            }
            list = newList;
            elements--;
            return true;
         }
     }
     return false;
}
public class SimpleArrayListTest {

    @Test
    public void testRemove() {
        final String ONE = "one";
        final String TWO = "two";
        final String THREE = "three";

        SimpleArrayList l = new SimpleArrayList();
        l.add(0,ONE);
        l.add(1,TWO);
        l.add(2,THREE);
        l.remove(TWO);
        assertEquals(2, l.size());
        assertTrue(l.contains(ONE));
        assertTrue(l.contains(THREE));
        l.remove(ONE);
        assertEquals(1, l.size());
        assertTrue(l.contains(THREE));
        l.remove(THREE);
        assertEquals(0, l.size());
    }

}
public boolean remove(String str)  {
    int previousLength =list.length;
    List<string> newList = new ArrayList<string>();
    for (int i=0;i<previousLength;i++) {
        final String s = list[i];
        if (str == s || s.contentEquals(str)) {
            continue;
         }
         list.add(s);
     }
     list = newList.toArray(new String[newList.size()]);
     //if same size then no element found and no delete operation => 5!=5 returns "false"
     //if different size then element is deleted => 4!=5 returns "true"
     return list.length!=previousLength;
}
public boolean remove(String str)  {
    int previousLength =list.length;
    List<string> newList = new ArrayList<string>();
    for (int i=0;i<previousLength;i++) {
        final String s = list[i];
        if (str == s || s.contentEquals(str)) {
            for (int j=i+1;j<previousLength;j++) {
                s = list[j];
                list.add(s);
            }
            break; //https://stackoverflow.com/questions/462373/difference-between-break-and-continue-statement
         }
         list.add(s);
     }
     list = newList.toArray(new String[newList.size()]);
     //if same size then no element found and no delete operation => 5!=5 returns "false"
     //if different size then element is deleted => 4!=5 returns "true"
     return list.length!=previousLength;
}