Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: If you can, show me what i am doing wrong

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default If you can, show me what i am doing wrong

    i am working on a assignment that has to do with array lists, it mainly has to do with adding new elements, telling then where it is it located, if the memory block is empty , ect. so far i have been having problems with my indexOf method which should display the array cell number that a input element E is in, and if it is not in there it should display a -1.

    public class MyArrayList<E> 
    {
    	private E[] data_store = (E[])new Object[2];
    	private int sizeofa = 0;
     
    	private void resize()// makes the array list bigger if need
    	{
    		E[] bigspacemaker = (E[])new Object[data_store.length * 2];
    		for(int x = 0 ; x< sizeofa ; x++)
    		{
    			bigspacemaker[x]= data_store[x];
    		}
    		data_store = bigspacemaker ;
    	}
     
    	public int indexOf(E element) // should returns the index of the given element. Returns -1 if element was not found.
    	{
    		boolean pass = false;
    		 ;
    		int o = 0 ;
     
    		while (element!= data_store[o])
    		{
    			o++;
    			if (element.equals(data_store[o]))//Eclipse says that the problem is here
    			{
    				pass = true;
    			}
     
    		}
     
     
    		if (pass == false)
    		{
    			int z = -1;
    			return z;
    		}
     
    		else
    		{
    			return o;
    		}
     
    	}
     
     
     
    	public void add(E element)// adds elements
    	{
    		if (sizeofa == data_store.length)
    		{
    			resize();
    		}
    		data_store[sizeofa] = element;
    		sizeofa++;
    	}
     
    	public boolean contains(E element)//checks to see if the array list contains a spesific element 
    	{
    		boolean a = false;
    		int count = 0 ;
    		while (count < sizeofa )
    		{
    			if ( data_store[count]  == element  )
    			{
    				a = true;
     
    			}
    			count++;
    		}
    		return a;
    	}
     
    	public boolean isEmpty()// checks if array cell is empty
    	{
    		if (data_store[0]==null)
    		{
    			return true;
    		}
    		else
    		{
    			return false;
    		}			
    	}
     
    	public E get(int index) 
    	{
    		return data_store[index];
    	}
     
    	public int size() 
    	{
    		return sizeofa;
    	}
     
     
     
    }

    and here is the code that tests it out

    public class MyArrayListDemo1 {
    	public static void main(String[] args)
    	{
    		MyArrayList<String> t = new MyArrayList<String>();
    		System.out.println("isEmpty? " + t.isEmpty());
    		t.add("Santa Maria");
    		t.add("Los Angeles");
    		t.add("Ventura");
    		t.add("Thousand Oaks");
    		System.out.println("isEmpty? " + t.isEmpty());
    		System.out.println("Contains Santa Maria? " + t.contains("Santa Maria"));
    		System.out.println("Contains Los Angeles? " + t.contains("Los Angeles"));
    		System.out.println("Contains Junk data 75? " + t.contains("Junk data 75"));
    		System.out.println("Contains Ventura? " + t.contains("Ventura"));
    		System.out.println("Contains Thousand Oaks? " + t.contains("Thousand Oaks"));
    		System.out.println("Contains Simi Valley? " + t.contains("Simi Valley"));
    		System.out.println("Contains Westlake? " + t.contains("Westlake"));
     
    		for (int x = 0; x < 500; x++)
    		{
    			t.add("Junk data " + x);
    		}
    		System.out.println("isEmpty? " + t.isEmpty());
    		System.out.println("indexOf Ventura: " + t.indexOf("Ventura"));
    		System.out.println("indexOf Junk data 75: " + t.indexOf("Junk data 75"));// it crashes right here
    		System.out.println("indexOf Santa Maria: " + t.indexOf("Santa Maria"));
    		System.out.println("indexOf Thousand Oaks: " + t.indexOf("Thousand Oaks"));
    		System.out.println("indexOf Junk data 499: " + t.indexOf("Junk data 499"));
    		System.out.println("indexOf Los Angeles: " + t.indexOf("Los Angeles"));		
    		System.out.println("indexOf Junk data 123: " + t.indexOf("Junk data 123"));
     
    		System.out.println("isEmpty? " + t.isEmpty());
    		System.out.println("--------");
    		System.out.println("Cool!  I didn't crash!");
    	}
    }
    please help me i am really confused on why it dosen't work
    sorry but i forgot to put in the error message:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 512
    at MyArrayList.indexOf(MyArrayList.java:28)
    at MyArrayListDemo1.main(MyArrayListDemo1.java:26)
    Last edited by gamerlovo; April 2nd, 2014 at 11:37 AM.


  2. #2
    Member
    Join Date
    Dec 2013
    Posts
    40
    My Mood
    Amazed
    Thanks
    2
    Thanked 11 Times in 11 Posts

    Default Re: If you can, show me what i am doing wrong

    Please post the errors you get when you run the codes. Btw, your use of o for 0 wouldn't work as a position holder in an array.
    Who holds the KEY to all knowledge?

Similar Threads

  1. Replies: 4
    Last Post: March 6th, 2014, 05:14 PM
  2. Why do my changes not show up?
    By Wolfone in forum AWT / Java Swing
    Replies: 2
    Last Post: September 29th, 2013, 07:17 AM
  3. Replies: 1
    Last Post: September 27th, 2013, 04:51 AM
  4. How to show a question again when a wrong answer is entered?
    By Henk in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2012, 02:02 PM
  5. Beginner: Show Image in Label when Results Show Up
    By Big Bundy in forum Java Theory & Questions
    Replies: 3
    Last Post: April 4th, 2011, 02:43 PM