簡體   English   中英

string arraylist刪除元素並再次將元素放在同一位置

[英]string arraylist delete an element and again put the element at the same position

我創建了字符串arraylist,其中我復制了數據庫中的所有數據。 然后我使用al.remove(1,null)從arraylist中刪除了一條記錄。 現在我想在沒有數據的位置添加記錄。 我的意思是我想在數據為空的位置添加數據。 我寫了al.set(位置,“新”),但它給了我運行時錯誤,即OutOfMemory。 請幫助我。 謝謝

import java.io.*;
import java.util.*;

public class DAOImpl implements DAO
{

    String xs[];

    List<String> al= new ArrayList<String>();
    int value;


    public void list()
    {
        try
        {
            BufferedReader br=new BufferedReader(new FileReader("insurance.db"));

            String next;
            while((next=br.readLine())!=null)
            {
                //System.out.println(next);
                al.add(next);
            }

            for(int i=0;i<al.size();i++)
            {
                System.out.print(i+1+"] ");
                String ar[]=(al.get(i)).split(":");
                for(int q=0;q<3;q++)
                {
                    System.out.print(ar[q]);
                    System.out.print("    ");
                }
                //System.out.println(al.get(i));
                System.out.println("");
            }

        }


        catch (FileNotFoundException ex)
        {
                    ex.printStackTrace();
            } 
        catch (IOException ex) 
        {
                    ex.printStackTrace();
        }


    }

    public String[] readRecord(int recNo)
    {

        String stream=(al.get(x-1));
        xs=stream.split(":");
        return xs;
    }


    public void deleteRecord(int recNo)
    {
        int del=recNo;
        al.set(del-1, null);

        for(int i=0;i<al.size();i++)
            {
                    if((al.get(i))==null)
                    {
                        continue;
                    }

                    else
                    {
                        System.out.print(i+1+"] ");
                        String ar[]=(al.get(i)).split(":");


                        for(int q=0;q<3;q++)
                        {

                            System.out.print(ar[q]);
                            System.out.print("    ");
                        }

                        System.out.println("");
                    }

            }   
    }


    public int addRecord()
    {

        for(int y=0;y<al.size();y++)
        {
            if((al.get(y))==null)
            {
                value=y+1;
            }
            if((al.get(y))==null)
            {
                al.add(y, "new");//m getting error here...
            }
        }
        for(int i=0;i<al.size();i++)
            {
                    if((al.get(i))==null)
                    {
                        continue;
                    }

                    else
                    {
                        System.out.print(i+1+"] ");
                        String ar[]=(al.get(i)).split(":");


                        for(int q=0;q<3;q++)
                        {

                            System.out.print(ar[q]);
                            System.out.print("    ");
                        }

                        System.out.println("");
                    }

            }
        return value;

    }

}

主要方法如下:

import java.io.*;
public class InsuranceMain
{
public static void main(String args[])throws Exception
{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    DAOImpl d=new DAOImpl();
    d.list();

    //deleterecord
    System.out.println("Enter Record Number to delete record:");
    int delete=Integer.parseInt(br.readLine());
    d.deleteRecord(delete);

    //addrecord

    d.addRecord();//m getting error here

    //readRecord
    System.out.println("Enter Record Number:");
    int s=Integer.parseInt(br.readLine());
    String as[]=d.readRecord(s);
    for(int v=0;v<as.length;v++)
    {
        System.out.println(as[v]);
    }

}
}

簡答:

改變這一行:

al.add(y, "new");//m getting error here...

al.set(y, "new");

原因:

如果你al.add(y,"new") ,那么y(包括)之后的所有元素都將向右移動。 所以下次你再次遇到null (y+1) ,你會添加另一個“new”,這個循環沒有結束。

如果你在這樣的for循環中改變列表的大小也不好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM