CodeGym /पाठ्यक्रम /All lectures for HI purposes /ArrayList और जेनरिक का उपयोग करने वाले उदाहरण

ArrayList और जेनरिक का उपयोग करने वाले उदाहरण

All lectures for HI purposes
स्तर 1, सबक 477
उपलब्ध

"मैं वास्तव में क्या उपयोगी है के साथ शुरू करूँ? अब आप कुछ तरीके देखेंगे कि ArrayList और जेनरिक को काम पर रखा जा सकता है:"

"उदाहरण 1:"

कीबोर्ड से संख्याओं की सूची पढ़ें
public static void main(String[] args) throws IOException
{
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in) );
    ArrayList<Integer> list = new ArrayList<Integer>() ;

    while (true)
    {
        String s = reader.readLine();
        if (s.isEmpty()) break;
        list.add(Integer.parseInt(s));
    }
}

"उदाहरण 2:"

उपरोक्त के समान, लेकिन सम संख्याओं को सूची के अंत में जोड़ा जाता है, विषम - इसकी शुरुआत में।
public static void main(String[] args) throws IOException
{
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    ArrayList<Integer> list = new ArrayList<Integer>();

    while (true)
    {
        String s = reader.readLine();
        if (s.isEmpty()) break;

        int x = Integer.parseInt(s);
        if (x % 2 == 0)  // Check that the remainder is zero when we divide by two
            list.add(x);         // Add to the end
        else
            list.add(0, x);      // Add to the beginning           
    }
}

"उदाहरण 3:"

5 से बड़ी सभी संख्याएँ हटाएं:
public static void main(String[] args) throws IOException
{
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in) );
    ArrayList<Integer> list = new ArrayList<Integer>();

    list.add(1);
    list.add(7);
    list.add(11);
    list.add(3);
    list.add(15);

    for (int i = 0; i < list.size(); ) // We moved the statement that increases i to inside the loop 
    { 
        if (list.get(i) > 5)
            list.remove(i);  // Don’t increase i if we deleted the current element   
        else
            i++;
    }
}

"उदाहरण 4:"

एक सरणी को दो भागों में विभाजित करें - सम और विषम संख्याएँ
public static void main(String[] args) throws IOException
{
    // Static initialization of the array
    int[] data = {1, 5, 6, 11, 3, 15, 7, 8};  

    // Create a list where all elements are Integers 
    ArrayList&ltInteger> list = new ArrayList&ltInteger> ();   

    // Use the array to fill the list
    for (int i = 0; i < data.length; i++) list.add(data[i]);  

    ArrayList&ltInteger> even = new ArrayList&ltInteger>();  // Even numbers
    ArrayList&ltInteger> odd = new ArrayList&ltInteger>();    // Odd numbers

    for (int i = 0; i < list.size(); i++)
    {
        Integer x = list.get(i);
        if (x % 2 == 0)    // If x is even
            even.add(x);   // Add x to the collection of even numbers  
        else
            odd.add(x);    // Add x to the collection of odd numbers
    }
}

"उदाहरण 5:"

सूचियों को मर्ज करें
public static void main(String[] args) throws IOException
{
    ArrayList<Integer> list1 = new ArrayList<Integer>();   // Create a list  
    Collections.addAll(list1, 1, 5, 6, 11, 3, 15, 7, 8);   // Fill the list

    ArrayList<Integer> list2 = new ArrayList<Integer>();
    Collections.addAll(list2, 1, 8, 6, 21, 53, 5, 67, 18);

    ArrayList<Integer> result = new ArrayList<Integer>();

    result.addAll(list1);   // Add all values from each list to the new list
    result.addAll(list2);

    for (Integer x : result)   // A fast way to loop over all elements, only for collections
    {
        System.out.println(x);
    }
}

"कूल! क्या डिएगो अब मुझे इसी तरह के अभ्यासों का एक ट्रक लोड देगा?"

"हां वो करेगा।"

टिप्पणियां
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION