-3

I need to make an array and add and delete from the end of the array.
The problem is I'v been asked to build Arraylist constructor to intialize the Array "list" but I got error on that What Should I Do ??!!!

01. Creating an instance of ArrayList(‐1). The list size will become 10
02. Adding 15 items. Only 10 will be added
03. Here is the list
A C E G I K M O Q S
04. Calling delete three times
05. Here is the list
A C E G I K M
06. Calling expandByTen
07. Here is the list
A C E G I K M
08. Adding 15 items.
09. Here is the list
A C E G I K M B D F H J L N P R T V X Z

import java.util.ArrayList;   

public class Main{

    /**
     * @param args
     */

     // create an array list

        char []list;
  int length;

  ArrayList(int maxSize)
   {
       if (maxSize<=0)
          length=10;
       else
           length=maxSize;

   }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        ArrayList arrayList;
        System.out.println("01. Creating an instance of ArrayList(‐1). The list size will become 10");
        arrayList = new ArrayList(‐1);
        System.out.println("02. Adding 15 items. Only 10 will be added");
        for (int i = 65; i < 96; i += 2) {
           arrayList.insertEnd((char) i);
        }
        System.out.println("03. Here is the list");
        arrayList.print();
        System.out.println("04. Calling delete three times");
        arrayList.deleteEnd();
        arrayList.deleteEnd();
        arrayList.deleteEnd();
        System.out.println("05. Here is the list");
        arrayList.print();
        System.out.println("06. Calling expandByTen");
        arrayList.expandByTen();
        System.out.println("07. Here is the list");
        arrayList.print();
        System.out.println("08. Adding 15 items.");
        for (int i = 66; i `enter code here`< 97; i += 2) {
           arrayList.insertEnd((char) i);
        }
        System.out.println("09. Here is the list");
        arrayList.print();
      }
    }
9
  • 1
    Please read the JLS and the documentation for ArrayList, then come back. What you have there is not remotely Java. Looks like you have grave misconceptions. Please explain, what does it mean: "I've been asked to build ArrayList constructor?" And, WTF is T?
    – Ingo
    Oct 25, 2013 at 12:35
  • I Need to get the out put 01. Creating an instance of ArrayList(‐1). The list size will become 10 02. Adding 15 items. Only 10 will be added 03. Here is the list A C E G I K M O Q S 04. Calling delete three times 05. Here is the list A C E G I K M 06. Calling expandByTen 07. Here is the list A C E G I K M 08. Adding 15 items. 09. Here is the list A C E G I K M B D F H J L N P R T V X Z
    – Noor noor
    Oct 25, 2013 at 12:39
  • 1
    When strangers ask you to "build ArrayList constructor" just say No!
    – Ingo
    Oct 25, 2013 at 12:40
  • Is your 10 pointed list what you think will happen, or what you want to happen? Oct 25, 2013 at 12:44
  • constructor, accepts one parameters. If the value of the parameter is negative or zero, default it to 10. Otherwise, set the variable maximumSize to the value of the parameter
    – Noor noor
    Oct 25, 2013 at 12:50

3 Answers 3

2

The class ArrayList already exists... You don't need to write such constructor: ArrayList(int maxSize).

Instantiate your ArrayList as you just did in the main method:

List myList = new ArrayList(); //don't add a -1 parameter

Then, cleverly use the ArrayList methods that you will find in the Java API.

myList.add(...);
myList.remove(...);
myList.size();
6
  • italic **But when I need to set the size of the list by the constructor !!!!!! ** code
    – Noor noor
    Oct 25, 2013 at 12:36
  • 1
    Welcome in the world of Java. An ArrayList automatically adds itself more space if needed. Don't worry about that. Oct 25, 2013 at 12:37
  • @Noornoor There is no maximum size for an arraylist, it will grow as large as needed (up to available memory), you can however set a default size if you wish Oct 25, 2013 at 12:37
  • 1
    @Noornoor Point 2 is not correct, all 15 will be added, also why do you want to use ArrayList(‐1), the -1 will cause an exception Oct 25, 2013 at 12:41
  • 1
    If you only want to add 10 items among the 15 given to you, just add a for loop that will only add the first 10 items. You should now be in the right way, now it's your turn to read some doc and think. Come back when you have tried something. Oct 25, 2013 at 12:42
2

Arraylist allocate size dynamically, increases the size while you adding the element to it. . So you can create arraylist like below.

 ArrayList arrayList= new ArrayList(10);

or

 ArrayList arrayList= new ArrayList();

You cant create ArrayList with size -1. replace new ArrayList(-1); to new ArrayList();

Remove the following useless code from your program. It wont work at all

 ArrayList(int maxSize)
   {
       if (maxSize<=0)
          length=10;
       else
           length=maxSize;

   }

There is no method print(); ,deleteEnd(); , insertEnd((char) i); in Arraylist

1
  • Its worth noting that using the initial size argument of the arraylist will have no effect on its actual operation, other than to make it more efficient if the size is set roughly equal to the number of objects it will contain Oct 25, 2013 at 12:43
0
  • I don't see where you create a new instance of the char array. Any reference to it will generate a NullPointerException
  • I don't see the declaration or implementation of ArrayList.insertEnd(..) which would generate a compilation error, so I'm assuming you have not included it here (same goes for deleteEnd and expandByTen etc).
  • Assuming you are creating the char[] instance somewhere with the length defined in the constructor, you set the size equal to 10 if the constructor argument is less than or equal to zero. In the main method, you instantiate an instance of ArrayList with a size of -1, which should (based on what little you have provided) create (somewhere) an instance of the list field with a size of 10. However, your first loop attempts to make 15 iterations, which will generate an IndexOutOfBounds exception at runtime if you aren't doing appropriate bounds checking in the insertEnd method.

I'm going out on a limb and guessing you are doing this as an assignment for school? If not, be aware that Java has a whole plethora of containers, including java.util.ArrayList, in the java.util package.

3
  • It's an assignment for school , but the problem is ... that i'm not able to explain my problem correctly , so now i'm stuck with this problem , i didn't get any hint for helping me to solve the assignment i just wonder if any one could help my by email so i can attach the assignment file ??!!!!
    – Noor noor
    Oct 25, 2013 at 12:57
  • If you are looking for someone to write the code for you, that won't be me. I'm willing to help explain why you are getting a specific output, but you'll have to workout the implementation details for yourself. It's the best way to get better at programming. If you can't do that, then you might want to look at participating in another subject.
    – MadConan
    Oct 25, 2013 at 13:01
  • You might get some ideas by reading the Java API for ArrayList. It doesn't give implementation details, but it does explain what functionality is provided (i.e. what vs how)
    – MadConan
    Oct 25, 2013 at 13:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.