Hitman
Hitman

Reputation: 87

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

i have created string arraylist in which i copied all the data from database. Then i removed one record from arraylist by using al.remove(1, null). Now i want to add record in the position on which there is no data. I mean i want to add data at the position where data is null. I did write al.set(position, "new") but its giving me run time error i.e. OutOfMemory. Pls help me. Thanks

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;

    }

}

and main method is as follow:

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]);
    }

}
}

Upvotes: 0

Views: 689

Answers (1)

Kent
Kent

Reputation: 195079

short answer:

change this line:

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

into

al.set(y, "new");

Reason:

if you al.add(y,"new"), then, all elements after y (inclusive) will be shifted right. So next time you meet the null again (y+1), you add another "new", do this loop no ending.

also it is not good if you changing the list's size within a for loop like this.

Upvotes: 2

Related Questions