{
  "video_id": "ByBnKLcRuco",
  "channel_slug": "amit.thinks",
  "channel_handle": "amit.thinks",
  "title": "Vectors in Java | Create Declare Intialize | Built-in Vector Methods | Java Tutorial | Amit Thinks",
  "duration_seconds": 427.0,
  "url": "https://www.youtube.com/watch?v=ByBnKLcRuco",
  "upload_date": "",
  "transcript": "In this lesson, we will learn what is a vector in \nJava. With that, we will also see some examples.\nLet us start. Vectors are growable arrays, and it \nis different from arrays. Arrays, once created,\ncannot be modified. But vectors can include \nmore elements because they are growable. It\nis a Java class, and to use it, you need to \nimport java.util.vector or java.util.*. Okay.\nOnce you set the size of an array, you cannot \nchange it. But vectors work like a dynamic\narray. So vectors are growable. So you can \nalways compare vectors with an ArrayList.\nVector is slower than ArrayList. Vectors are \ngenerally considered outdated. But use vectors\nif you need a thread-safe dynamic array without \nexternal synchronization. Let us see how we can\ncreate a vector in Java. To create a vector, you \ncan use the following two ways. The first one is\nrecommended. In the first one, you use generics. \nIt means that the data type is visible. In the\nexample where you can see \"String\", it means we \nwill only store string objects. Type safety is\nenforced at compile time. For example, if you \ntry to add an integer to this string vector,\nthe compiler will throw an error. We have a 2nd \nway, also, which is not generally recommended. In\nthis, the data type is not visible. So this is \na raw type. Here I have also shown an example:\n\"Vector v\" is equal to, \"new Vector\". Okay. In \nthis, the type safety is not enforced at compile\ntime. So since the type is not visible, you could \naccidentally mix types. So this is generally\navoided. In our example, we will use the first \nmethod of declaring and creating vectors. Now,\nlet us see an example of vectors in Java. Here, \nwe will understand some of the built-in methods of\nvectors, in which we will add an element and get \nthe size of the elements. We will also play around\nwith indexes, that is, to insert an element, add a \nspecific index, to remove an element from a given\nindex, and others. Let us start. So here is our VS \ncode. We already created, \"AmitJavaProject\". Okay,\nhere it is. And we added our files. So this is \nour vector example. I told you to use vector,\nyou can either use import java.util, or if you \nwant to add all of them, you can just use *. But\nI'll directly mention the vector class. Okay. So \nthis is our class definition. This is the main()\nfunction where the execution begins. So here we \nare creating a String vector. I already told you\nthat we'll be using the first method, and after \ncreating it, what I did, I just got the size of\nthis vector. This is an empty vector because we \ndid not mention the size. After that, we created\nanother vector in which we mentioned the size. \nSo let us first see the output of these two. Run\nHere, the size of both of them is zero. Why? \nBecause we did not add any elements to this. Okay,\nwhen we add the element, the size will be shown \naccordingly. But we have a space for 10 elements\nnow. But since vectors are growable, we can also \nadd more elements. So I have added 11 elements\nhere. The size is 10, but I have added 11, and \nthere will be no error. So we are displaying the\nvectors here and the size of the vectors \nnow. So let us see the output again. Here\nis the output. All the elements are visible. \nAnd now the size of the vector is 11. Okay,\nthis 11 is coming from here. It is 11. But \nwe have set 10 here. That means vectors are\ngrowable arrays. After that, we are using a \nfor loop so that we can display the elements.\nOkay. So here we are finding elements at index \n012, and we are printing using the elementAt()\nmethod. This will print each and every element at \na specific index. So what will happen when the for\nloop begins? So the for loop is from i is equal \nto 0 to subjects.size(), that is it was 11. One\nby one, it will take the value of i and print at \nthe index that is elements at index 0 will be PHP,\nthat is the following, and to add the elements, \nwe have used the addElement() method. So at first,\nPHP will get printed, similarly, the loop \nwill go on, then, Maths, Java, English,\nand the rest of them will go until index 10, that \nis position 11. What are we doing after that? We\nare fetching a specific element using an index. \nSo, using the elementAt() method, it will fetch\nthe element at the 5th index. What is the element \nat the 5th index 0 1 2 3 4 5 that is it will print\n\"IT\". So, for this, it printed \"IT\". After that, \nwe are inserting an element at a specific index.\nSo we are inserting \"GenAI\", at index 2, and there \nwon't be any error because vectors are a growable\narray. Now we are printing the updated vector. \nSo what is the updated vector here? At index 2,\nwe added \"GenAI\". So now, \"GenAI\" will be here, \nand the rest of them will go after that. That is,\n\"Java\", \"English\", up to index 11. Since we added \none more element, the size of our vector is 12.\nWe are printing the size here again. The size \nis now 12. Now we will find a specific element\nusing the contains() method. Does your vector \nhave a subject \"Algorithm\"? It will check. The\nelement you want to check comes here. Okay. So \nif the value is there, the output will be true.\nIt is true. Does your vector have the subject \n\"Algorithms\"? Yes. Here it is. So it is showing\ntrue. Now we are removing elements at a \nspecific location that is at index 2, 5,\n7, and 8 using the removeElementAt () method. \nSo after that, we are printing the vector again\nusing elementAt(), and the loop is from 0 to the \nsubjects.size(). Since we removed four elements,\nthe size will now be 8. Here, \nall these elements are removed.\nNow the size of the vector is 8. So in this \nway, we can work with vectors. We saw some\nbuilt-in methods. After running, the class \nfile also generated. Thank you for watching.",
  "transcript_chars": 5705,
  "ingested_at": "2026-06-18T22:33:54.708026+00:00",
  "source": "channel",
  "yt_meta": {
    "view_count": null,
    "like_count": null,
    "channel_id": null,
    "categories": null,
    "tags": null
  }
}