<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Divyansh Pratap Singh </title>
    <description>The latest articles on DEV Community by Divyansh Pratap Singh  (@devdivyansh).</description>
    <link>https://dev.to/devdivyansh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F918558%2Fc5adb056-cf90-4a78-852d-50d6e0433728.png</url>
      <title>DEV Community: Divyansh Pratap Singh </title>
      <link>https://dev.to/devdivyansh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devdivyansh"/>
    <language>en</language>
    <item>
      <title>understanding ArrayList</title>
      <dc:creator>Divyansh Pratap Singh </dc:creator>
      <pubDate>Fri, 02 Sep 2022 00:39:06 +0000</pubDate>
      <link>https://dev.to/devdivyansh/understanding-arraylist-3ieh</link>
      <guid>https://dev.to/devdivyansh/understanding-arraylist-3ieh</guid>
      <description>&lt;h1&gt;
  
  
  Understanding the concept of ArrayList :
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;Authors : Divyansh Pratap Singh (&lt;a href="https://mobile.twitter.com/dev_pratap3250/"&gt;twitter&lt;/a&gt;)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Array List vs Array :
&lt;/h1&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Array List&lt;/th&gt;
&lt;th&gt;Array&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;they have variable size&lt;/td&gt;
&lt;td&gt;they have fixed size&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;they have non-continous memory allocation&lt;/td&gt;
&lt;td&gt;they have continous memory allocation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;they can store only objects&lt;/td&gt;
&lt;td&gt;they can store primitive data-types ex. int , float&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h1&gt;
  
  
  Implementing ArrayList
&lt;/h1&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    `import java.util.ArrayList;
    public class arrayy_list{
        public static void main(String[]args){

            ArrayList&amp;lt;Integer&amp;gt; list = new ArrayList&amp;lt;Integer&amp;gt;();

        }
    }
   `
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;Similarly for declaring String&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;ArrayList&amp;lt;String&amp;gt; name = new  ArrayList&amp;lt;String&amp;gt;();&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Adding element  in ArrayList
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;we use add function to add an element in ArrayList&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;ArrayList&amp;lt;Integer&amp;gt; list = new ArrayList&amp;lt;Integer&amp;gt;();&lt;br&gt;
list.add(3);&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Adding elements 3,2,5,0 --&amp;gt; below is the complete code for adding element&lt;br&gt;
'''&lt;br&gt;
import java.util.ArrayList;&lt;br&gt;
        public class arrayy_list{&lt;br&gt;
            public static void main(String[]args){&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            ArrayList&amp;lt;Integer&amp;gt; list = new ArrayList&amp;lt;Integer&amp;gt;();

            list.add(3);
            list.add(2);
            list.add(5);
            list.add(0);
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;`&lt;/p&gt;

&lt;h1&gt;
  
  
  Accessing elements of ArrayList :
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;to access element in ArrayList we  use get function . Inside get function we  pass the index position of the element .&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;list.get(0);&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Gives us : 3&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Adding element in middle of ArrayList :
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;add  function adds an element at the end of the ArrayList ; to add element at any position in ArrayList we use add function and pass 2 parameters ;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;list.add(&amp;lt;indexposition&amp;gt;&amp;lt;element&amp;gt;);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;list.add(0,9);&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Result : [9,3,2,5,0]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Changing element in ArrayList :
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;to change a value in ArrayList we  use set function . The function takes  2 parameters ;  1st is the index position of the element you want to change and the 2nd parameter is the new element.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;list.set(0,8);&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Result : [8,3,2,5,0]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  removing an element from ArrayList :
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;to remove a element from ArrayList we use remove function and pass the element index position as the  parameter.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;list.remove(0);&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Result : [3,2,5,0]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  printing complete ArrayList
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;System.out.println(&amp;lt;ArrayList name &amp;gt;);&lt;br&gt;
System.out.println(list);&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Result : [3,2,5,0]&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>arraylist</category>
      <category>dsa</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>finding size of Array in C</title>
      <dc:creator>Divyansh Pratap Singh </dc:creator>
      <pubDate>Wed, 31 Aug 2022 13:16:45 +0000</pubDate>
      <link>https://dev.to/devdivyansh/finding-size-of-array-in-c-4l87</link>
      <guid>https://dev.to/devdivyansh/finding-size-of-array-in-c-4l87</guid>
      <description>&lt;h2&gt;
  
  
  What  we  will learn ?
&lt;/h2&gt;

&lt;p&gt;In this read you are going to learn how to find the size  of the array in C  programming language and the concepts behind its working .&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction :
&lt;/h2&gt;

&lt;p&gt;As in any other  programming language like Java we have length property of array through which we  can easily find out the size or the length of the  array but in c arrays do not have  any such properties  through which we can easily find  out the size of the array . Though there are two ways through which we can find out the length or size  of  the array in our  C programming language ; but in comparison to other languages like of java it's  a bit  complicated . So , in this  blog  you are going to learn how to find the size along with the  working and the reason why  it is  happening so .&lt;/p&gt;

&lt;h2&gt;
  
  
  Method  1
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Using a  constant variable size
&lt;/h3&gt;

&lt;p&gt;So , we  will  be defining an constant variable 'Size' which would store  the size  or the length of the  array . Suppose we  have to store  five values in our array so  the value of our size variable is 5 .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const  int Size  = 5 ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here  , in the above  line const  is  a keyword  that  declares the variable Size as  constant i.e we  cannot change the value of Size in the upcoming  program .&lt;/p&gt;

&lt;p&gt;Now , the next step is to define and initialize our array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; int arr[Size] = { 1 , 2 , 3 , 4, 5};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;here ; in the above line we declared an array of name 'arr' and passed the values  inside the array ; also do  remember to pass the size  of the array .&lt;/p&gt;

&lt;p&gt;code of c  program to print the  elements of  Array :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;conio.h&amp;gt;
void main(){
clrscr();

const int size = 4;
int arr[size] = {1,2,3,4};

for(int i = 0 ;i&amp;lt;size ;i++){
printf("%d \n " , arr[i]);
}
getch(); }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;









&lt;h3&gt;
  
  
  Additional  info :
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Case 1 :
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const  int  size = 4;
  int arr[size] = { 1,2,3 };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code we have set the size of the array as 3 and inside the array we are only passing 3 values . Will we get an Error ? &lt;br&gt;
&lt;br&gt;&lt;br&gt;
No , actually what  happens  when we  declared size as 4 then there are 4 blocks created where you are only  entering values  in 3  blocks so , the last block remains  empty . So , the  empty  block is filled by 0 by default during the run time ._&lt;/p&gt;
&lt;h3&gt;
  
  
  Case 2 :
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const  int  size  = 2 ;
int arr[size] = {1,2,3}; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;here  , in the above  code  we have set our size as  2  so now we have  2 blocks and  later in the program we  are storing 3  values ; we have 2 blocks and we are storing 3  values so this is going to throw us an error ._&lt;/p&gt;




&lt;h2&gt;
  
  
  Method  2
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Using sizeof operator
&lt;/h3&gt;

&lt;p&gt;So , In the above section we have learnt how to find out  the size of array using a  constant variable and now in this section we  are  going to find out the size  or length using sizeof operator .&lt;/p&gt;

&lt;p&gt;So , before  jumping  in to the implementation part first let's us  understand  what is sizeof operator .&lt;/p&gt;

&lt;p&gt;The  sizeof  is an  unary operator in c and c++ . It is  basically used  to compute the  size  of the operand .The  result of sizeof operator is an unsigned integral type which is denoted by  size_t .&lt;br&gt;
sizeof  operand can  be  used  with any data type , primitive  data type example int , float as  well  as compound data type example Structures .&lt;/p&gt;
&lt;h3&gt;
  
  
  Usage
&lt;/h3&gt;

&lt;p&gt;when sizeof() is  used   with datatypes like int or float then it returns the  memory occupied by them .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;printf("%lu" , sizeof(int));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this  would give 4 as  an output&lt;/p&gt;

&lt;p&gt;when sizeof()  is used  with an expression then it would return the  size  of  the expression .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;float  a = 1.2;
double b = 1.0;
printf("%lu" , sizeof(a+b));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this  would give 8 as an output.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Returning  back to our Question :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; int arr = [1,2,3,4];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;here  in the  above  code i had  declared and initialized an array and now i want to print  the size of the array , so let's find out the size using  sizeof() operator .&lt;br&gt;
So , for better understanding i am storing the size  in a variable named  size , if you wish then you  can directly print the size .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int arr[] = {1,2,3,4,5};
int size = sizeof(arr) / sizeof(arr[0]); 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now , you might be  wondering why i had divided  the sizeof(arr) with sizeof(arr[0]) , let me explain .&lt;/p&gt;

&lt;p&gt;sizeof(arr) operator  will give  you the total memory occupied by the array arr and  similarly sizeof(arr[0]) will return you the memory occupied by the single element so by  dividing the total space by the space  ocupied by 1 element then we will get the number of elements .&lt;/p&gt;

&lt;p&gt;You  can understand this way , suppose a guy  paid 50 INR in total for buying chocolates , now you are told  that 1 chocolate was for 10 INR and now tell me the number  of chocolates the guy bought .&lt;br&gt;
(40/10) which is  4  chocolates . This very same concept we are using there in sizeof().&lt;/p&gt;

&lt;p&gt;Important : sizeof operator do not returns  size it returns the memory occupied by the operand .&lt;/p&gt;

&lt;p&gt;Entire code for printing  the size of Array using sizeof operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;conio.h&amp;gt;

void main(){
clrscr();
int arr[] = {1,2,3,4,5};

int size = sizeof(arr) / sizeof(arr[0]);

printf("%d" , size);
getch()  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Thankyou , for giving it a read I  hope  you understood the concept and  if you have  any doubt please  comment below .&lt;/em&gt;&lt;/p&gt;

</description>
      <category>c</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
