Blog Oxunjon Gaybullayeva

Introduction to ArrayList in Java!!!

Arraylistdan foydalanish qanday bo'ladi

Hello dear readers of my blog. Let’s start learning data structures in the Java programming language with ArrayList. In this article, you will learn what ArrayList is in Java and how to use it in programming.

The Java API (application programming interface) provides a number of data structures called collections, which are used to group a group of objects.

These classes provide effective methods for organizing, storing, and storing your data without requiring any knowledge of how data is collected. This reduces the development time of the program.

We can use arrays to store a sequence of objects. However, Arrays do not automatically resize themselves in time to accommodate additional elements. ArrayList of the Collection class (found in the java.util package) offers a convenient solution to this problem – it can dynamically change its size to accommodate more elements.

ATTENTION ADVERTISEMENT! A video course on Java programming languages is ready. You can read information about the video course by clicking on the image below:

Here ” T ” is written instead of the place holder – that is, when declaring a new ArrayList, the type of element that you want to store in this ArrayList. This is similar to specifying a type for an Array declaration, except that NONprimitive types can be used with collections classes. For example:

ArrayList list; declares ” list ” as an ArrayList collection that can only hold String data.

ArrayList number; declares ” number ” as an ArrayList collection that can only store data of type Integer.

Classes with placeholders of this type that can be used with any type are called generic classes. I will provide information about generic classes in the next articles.

Below is a list of some common methods used with ArrayList.

add() – Adds an element to the end of the ArrayList.

clear() – deletes all elements from the ArrayList.

contains() – Returns true if the ArrayList contains the specified element; Otherwise, (False) returns false.

get() – Returns the element at the specified index.

indexOf() – Returns the index of the first occurrence of the specified element in the ArrayList.

remove() – Removes the first occurrence of the specified value or element in the specified directory.

size() – Returns the number of elements stored in the ArrayList.

trimToSize() – Convert the size of the ArrayList to the current number of elements

The following program demonstrates some of the capabilities of ArrayList. Line 6 creates an ArrayList that accepts a new String type.import java.util.ArrayList;

import java.util.ArrayList;
public class ArrayList{
public static void main( String[] args )
{
// Create an ArrayList
ArrayList< String > items = new ArrayList< String >();
elements.add( " red " ); // add an element to the list
elements.add( 0, " yellow " ); // Insert value at index 0
System.out.print( "Display list contents using loop:");
// Display the colors in the list
for ( int i = 0; i < items.size(); i++ )
System.out.printf( " %s", items.get( i ) );
// show colors in the display method
display( items, "\nDisplaying a list using an enhanced for loop statement" );
elements.add( " green " ); // add to the end of the list green .
elements.add( " yellow " ); // add yellow to the end of the list
display( items, "A list containing two new items:" );
elements.remove( " yellow " ); // delete the first yellow element
display( items, "Delete the first instance of yellow:" );
elements.remove( 1 ); // delete element at index number 1
display( items, "green removed:" );
// Check if there is a value in list
System.out.printf( "\"red\" %s in list\n", elements.contains( " red " ) ? "": "none " );
// Display the number of elements in the list
System.out.printf( "Size: %s\n", items.size() );
} // main method is finished
// Display the ArrayList elements in the console
public static void display( ArrayList< String > items, String title )
{
System.out.print( title ); // display title
// display any element
for ( String element : elements )
System.out.printf( " %s", item );
System.out.println();
} // display method is finished
}

The result:

Show list contents using loop: yellow red

Displaying a list through an enhanced for loop statement is yellow red

A list containing two new elements: yellow red green yellow

Delete the first example of yellow: red green yellow

green off:  red  yellow

” in the list

Size: 2

EXPLANATION:

The add() method adds elements to the ArrayList (7,8 – row). The add() method with one argument adds its element to the end of the ArrayList (line 7). The add() method, which takes two arguments, inserts a new element at the specified location. The first element is the index number. As with arrays, collections start at index 0. The second argument is the value to insert into that index.

Indicators (index) of all subsequent elements increase together. Inserting an element is usually a slower process than adding an element to the end of an ArrayList.

Lines 11,12 show the elements in the ArrayList. The size() method returns the current number of elements in the ArrayList. The ArrayList method get() (line 12) contains the element at the specified index. Line 14 displays the elements again by calling the display() method (lines 28-35).

Lines 15,16 add two more elements to the ArrayList, then line 17 redisplays the elements to confirm that two elements have been added to the end of the collection.

The remove() method is used to delete an element with a special value (line 18). It deletes only the first such element. If the ArrayList does not contain such a value, the remove() method does nothing. Another version of this method deletes the element at the specified index (line 20). When an element is deleted, the index of all elements after the deleted element is reduced together.

Line 23 uses the contains() method to check if there is an element in the ArrayList. The contains() method returns true if an element is found in the ArrayList, and false otherwise. This method compares (equals) its argument with each element of the ArrayList, so using the contains() method on large ArrayLists can be inefficient. Line 25 shows the ArratList size.

Now you know what ArrayList is and how to work with it. If there is something you don’t understand, feel free to ask in the comments!

And you can also leave an opinion about which topics of the Java programming language you do not understand well, and then I will prepare an article according to your request.

See you in the next article!

Рейтинг
( No ratings yet )
Oxunjon G'aybullayev/ author of the article
Понравился этот пост? Поделись с друзьями:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: