473,417 Members | 1,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,417 software developers and data experts.

How do I test the implementation of my ArrayList class in netbeans?

Hi i have created a class called myArrayList with many methods, I'm now trying to run this class but im getting the error (illegal start of expression), could someone please help me out because im really stuck.

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.util.ArrayList;
  3.  
  4.  
  5. public class myArrayList {
  6.  
  7.     /**
  8.      * @param args the command line arguments
  9.      */
  10.     public static void main(String[] args)
  11.     {
  12.  
  13.         ArrayList myArrayList = new ArrayList();
  14.  
  15.  
  16.     private ArrayList[] accounts;
  17.     private int numAccounts;
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24. //This is my first constructor which has an empty list with an initial capacity of ten.
  25.     public myArrayList()
  26.     {
  27.             accounts = new ArrayList[10];
  28.             numAccounts = 0;
  29.         }
  30.  
  31. //This is my second Constructor which has an empty list with an initial cappacity of 'initialCapacity'.
  32.     public myArrayList(int initialCapacity)
  33.     {
  34.             accounts = new ArrayList[initialCapacity];
  35.             numAccounts = 0;
  36.         }
  37.  
  38.  //This will Return the number of elements in the List.
  39.         public int size()
  40.     {
  41.         return numAccounts;
  42.         }
  43.  
  44.  //This Tests will show if the list has no elements, and returns true if the list is empty and false otherwise.
  45.         public boolean isEmpty()
  46.     {
  47.         if(numAccounts == 0)
  48.             return true;
  49.         else
  50.             return false;
  51.         }
  52.  
  53. /**
  54. * Returns true if this list contains the specified element.
  55. * @param b element whose presence in this List is to be tested.
  56. * @return true if the specified element is present; false otherwise.
  57. */
  58.         public boolean contains(ArrayList b)
  59.     {
  60.         int index = find(b);
  61.         if(index == -1)
  62.         {
  63.             return false;
  64.         }
  65.         else
  66.             return true;
  67.         }
  68.  
  69.  //This Removes all of the elements from this list. The list will thus be empty.
  70.         public void clear()
  71.     {
  72.         for(int i = 0; i != numAccounts; i ++) {
  73.             accounts[i] = null;
  74.         }
  75.         numAccounts = 0;
  76.         }
  77.  
  78.  /**
  79.  * Replaces the element at the specified position in this list with the specified element.
  80.  * @param index index of element to replace.
  81.  * @param b element to be stored at the specified position.
  82.  * @return the element previously at the specified position.
  83.  */
  84.         public ArrayList set(int index, ArrayList b)
  85.     {
  86.             accounts[index] = b;
  87.             return null;
  88.         }
  89.  
  90. /**
  91. * Inserts the specified element at the specified position in this list.
  92. * Shifts the element currently at that position (if any) and any
  93. * subsequent elements to the right (adds one to their indices).
  94. * @param b element to be inserted.
  95. * @return
  96. */
  97.         public boolean add(ArrayList b)
  98.     {
  99. //if the array is full
  100.             if(numAccounts == accounts.length)
  101.         {
  102.                     ArrayList [] newData = new ArrayList[accounts.length + 10];
  103.                     System.arraycopy(accounts, 0, newData, 0, accounts.length);
  104.                     accounts = newData;
  105.         }
  106.                 accounts[numAccounts] = b;
  107.                 numAccounts ++;
  108.                 return true;
  109.         }
  110.  
  111. /**
  112. * Appends the specified element to the end of this list
  113. * inserts the element b at the index 'index'
  114. */
  115.         public void add(int index, ArrayList b)
  116.     {
  117. //if the array is full
  118.             if(numAccounts == accounts.length)
  119.             {
  120.                 ArrayList [] newData = new ArrayList[accounts.length + 10];
  121.                 System.arraycopy(accounts, 0, newData, 0, accounts.length);
  122.                 accounts = newData;
  123.             }
  124.             if(index != numAccounts)
  125.             {
  126.                 System.arraycopy(accounts, index, accounts, index + 1, numAccounts - index);
  127.             }
  128.             accounts[index] = b;
  129.             numAccounts ++;
  130.         }
  131.  
  132. /**
  133. * Removes the element at the specified position in this list. Shifts
  134. * any subsequent elements to the left (subtracts one from their indices).
  135. * @param index the index of the element to removed.
  136. * @return the element that was removed from the list.
  137. */
  138.         public ArrayList remove(int index)
  139.     {
  140. //if the index given is invalid
  141.             if(0 < index || index >= numAccounts)
  142.         {
  143.                     throw new RuntimeException("remove: invalid index");
  144.                 }
  145.             System.arraycopy(accounts, index + 1, accounts, index, numAccounts - 1);
  146.             numAccounts --;
  147.             return null;
  148.         }
  149.  
  150. /**
  151. * Removes a single instance of the specified element from this list,
  152. * if it is present (optional operation). More formally, removes an
  153. * element e such that (o==null ? e==null : o.equals(e)), if the list
  154. * contains one or more such elements. Returns true if the list contained
  155. * the specified element (or equivalently, if the list changed as a result
  156. * of the call).
  157. * @param b element to be removed from this list, if present.
  158. * @return true if the list contained the specified element.
  159. */
  160.         public boolean remove(ArrayList b)
  161.     {
  162.             int index = find(b);
  163.             if(index == -1)
  164.             {
  165.                 return false;
  166.             }
  167.             else
  168.             {
  169.                 remove(index);
  170.                 numAccounts --;
  171.                 return true;
  172.             }
  173.         }
  174.  
  175. /**
  176. * Returns the element at the specified position in this list.
  177. * @param index index of element to return.
  178. * @return the element at the specified position in this list.
  179. */
  180.         public ArrayList get(int index)
  181.     {
  182.             if(0 < index || index >= accounts.length)
  183.             {
  184.                 throw new RuntimeException("remove: invalid index");
  185.             }
  186.             else
  187.             return accounts[index];
  188.         }
  189.  
  190. //returns index of ArrayList b, returns -1 if not found
  191.         private int find(ArrayList b)
  192.         {
  193.             for(int i =0 ; i != numAccounts; i ++)
  194.             {
  195.                 if(accounts[i] == b)
  196.                 return i;
  197.             }
  198.             return -1;
  199.         }
  200. }
  201. }
  202.  
Oct 23 '10 #1
0 1121

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: roy | last post by:
The following is a simple example I wrote. I want function makenull() to set the arraylist null. However, in this example, after coming back from makenull() function, s is still not null. I got...
0
by: Tim Haughton | last post by:
I've just released an article on using Test Driven Development with C# and Windows Forms. GUI's are often difficult to test, so I thought it might be of interest. The article along with the...
1
by: bor_kev | last post by:
Hi, I'm using Visual C++ .NET 2003 and I want to use the ArrayList class in order to put managed classes in it. For instance , i wrote : (I created an empty .NET project) #using...
9
by: Leon | last post by:
I have a webform in which when the user press generate button the form generate six unique ramdon numbers, the user can also type these six numbers in manually in any order. however, the user can...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
3
by: Arjen | last post by:
Hi, public class test { public ArrayList myList = new ArrayList(); } test myTest = new test(); myTest.myList.Add(<someobject>); This results in an error.
18
by: Wiktor Zychla [C# MVP] | last post by:
Following code was supposed to sort an ArrayList of strings while keeping one "fixed" element at the last position. However, the code hangs in .NET 1.1. I think that answering these questions...
2
by: Ryan Liu | last post by:
Hi, There is a method Test() implemented in base class and override in subclass. In base class itself, how to make sure this.Test() will just call implementation in base class only? ...
3
by: =?Utf-8?B?RWl0YW4=?= | last post by:
Hello, I need an array of objects. I am considering using either ArrayList Class or List Generic Class. Any guidelins regarding when to use one or the other? Thanks Eitan
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.