Hi guys,

i m new for ths java progrmming.Please me the diff between array and arraylist.

Both are dynamically allocated values.any added mechansims in the arraylist.
Please let me asap.

Regards,
Pradeep

Recommended Answers

All 16 Replies

Member Avatar for harsh2327

Hi

there are many such classes in Java which can be used in similar ways and have similar functionality. eg.You could also use Vector class for dynamic allocation. You could use either of them depending your comfort level.

One difference between array and ArrayList class is that
in array, datatypes of all elements has to be same (like all elements should be int or float or char).
However, in ArrayList (or Vector class for that matter), you can insert Objects. eg. First element could be an "int", second element could be a "float", third could be a "string" or even a "user defined object".

Despite previous post, arrays can also contain objects (depending on how they are declared) Object[] x = new Object[];.
arrays are a fixed-size list of members). They not have methods etc. All you can do is refer tp individual elements.

ArrayList is a Class. Instances of ArrayList can contain all kinds of objects (depending on how they are declared). They have a good number of public methods for adding/retrieving/removing elements, and are generally a lot more powerful than arrays. In particular they re-size automatically to fit whatever number of elements you put in them, and "close up the gap" when you remove elements from the middle. Vector is almost the same, but has the additional advantage of being synchronised, so they are much safer to use in multi-threaded programs.

commented: Thanks for pointing out my mistake +2
Member Avatar for harsh2327

Hey JamesCherrill,

thanks a lot for correcting me. Declaring an object array didn't come to my mind

Hi harsh2327
Don't worry, we've all done somethig like that at tleast once.

ps You may also want to reconsider this statement

However, in ArrayList (or Vector class for that matter), you can insert Objects. eg. First element could be an "int", second element could be a "float", third could be a "string" or even a "user defined object".

I'd be interested to see a piece of code that adds an int or a float to an ArrayList.

Member Avatar for harsh2327

Hi harsh2327
Don't worry, we've all done somethig like that at tleast once.

ps You may also want to reconsider this statement

I'd be interested to see a piece of code that adds an int or a float to an ArrayList.

import java.util.*;

public class temp {
	public static void main(String args[]) {
    	ArrayList<Object> a = new ArrayList<Object>();
    	a.add(2);
    	a.add(3.3);
    	a.add("Hi");
    	System.out.print(a.get(0) +"  " + a.get(1) + "  " + a.get(2));
	}
}

There you go

Aha!
If you add
System.out.print(a.get(0).getClass());
or
int i = a.get(0);
you'll see that you haven't added an int at all!
(Auto boxing/unboxing has disguised what really happens)

Member Avatar for harsh2327

Yes, it says you have added Integer Class, a Double class and a String Class

import java.util.*;

public class temp {
	public static void main(String args[]) {
    	ArrayList<Object> a = new ArrayList<Object>();
    	a.add(2);
    	a.add(3.3);
    	a.add("Hi");
    	System.out.print(a.get(0).getClass() +"  " + a.get(1).getClass() + "  " + a.get(2).getClass());
	}
}

The output of this is
class java.lang.Integer class java.lang.Double class java.lang.String

This probably happens because you are saying a.get(0).getCass().
int and double are not classes. Thus, autoboxing is coming into picture.

It didn't turn them into classes because you called getClass()!
Did you try to assign the element to an int? It won't even compile.
ArrayList only holds Objects, not primitives.

Member Avatar for harsh2327

It didn't turn them into classes because you called getClass()!
Did you try to assign the element to an int? It won't even compile.
ArrayList only holds Objects, not primitives.

Yes I did !!

int t = Integer.parseInt(a.get(0)+"");

I think we've hijacked this thread long enough, don't you?
See you next time.
;-)

Thanx all of u for the response,I heard arraylist have some additional features are 1)Sorting,
2)Filtering
3)Bookmarking like that.
Can we do these operations by using Arrray ?Please let me know the some additonal information,This is great helpful to me

The quick answer is that you can do all of those things using arrays.
In fact, the ArrayList class uses private arrays to do all its work.
The difference is that ArrayList has many methods already written to help you. With arrays you will have to write them yourself.
In my opinion, using arrays in Java is almost always a bad decision; ArrayLists and Vectors are far more powerful and easier to work with.

It will be like....

import java.util.*;

public class temp {
    public static void main(String args[]) {
        ArrayList<Object> a = new ArrayList<Object>();
        a.add(2);
        a.add(3.3);
        a.add(

Stop reviving old thread! Look at the last date posted before replying. Also, your code is not completed + not in a code tag!

array has fixed size and arraylist is immutable(u can change the size but every tym a new array gets created implicitly)

rashma123: welcome to DaniWeb. Your contributions are welcome, but please check the dates of threads befreo replying. This question was answered 2 years ago.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.