0% found this document useful (0 votes)
62 views4 pages

Java ArrayList Operations and Examples

This document discusses Java code examples for working with ArrayLists and arrays. It includes code to: 1. Convert an ArrayList to an array by iterating through the ArrayList and adding each element to the array. 2. Sort an ArrayList in ascending or descending order using the Collections.sort() and Collections.reverse() methods. 3. Find the minimum and maximum elements of an integer array using Collections.min() and Collections.max(). 4. Perform time conversion that takes in a time in 12-hour format and prints it in 24-hour format. 5. Modify an array to calculate the running sum of prior elements in each index.

Uploaded by

Islamic India
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views4 pages

Java ArrayList Operations and Examples

This document discusses Java code examples for working with ArrayLists and arrays. It includes code to: 1. Convert an ArrayList to an array by iterating through the ArrayList and adding each element to the array. 2. Sort an ArrayList in ascending or descending order using the Collections.sort() and Collections.reverse() methods. 3. Find the minimum and maximum elements of an integer array using Collections.min() and Collections.max(). 4. Perform time conversion that takes in a time in 12-hour format and prints it in 24-hour format. 5. Modify an array to calculate the running sum of prior elements in each index.

Uploaded by

Islamic India
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ArrayList

ArrayList<Integer> arrli = new ArrayList<Integer>(n);

 [Link](i);

[Link](3);

[Link](i)
// Java program to convert a ArrayList to an array
// using get() in a loop.
import [Link].*;
import [Link];
import [Link];
  
class GFG
{
    public static void main (String[] args)
    {
        List<Integer> al = new ArrayList<Integer>();
        [Link](10);
        [Link](20);
        [Link](30);
        [Link](40);
  
        Integer[] arr = new Integer[[Link]()];
  
        // ArrayList to Array Conversion
        for (int i =0; i < [Link](); i++)
            arr[i] = [Link](i);
  
        for (Integer x : arr)
            [Link](x + " ");
    }
}

[Link](list);// ArrayList in ascending order


[Link](list);//ArrayList in descending

// creating Arrays of String type


            String a[] = new String[] { "A", "B", "C", "D" };
  
            // getting the list view of Array
            List<String> list = [Link](a);
  
            // printing the list
            [Link]("The list is: " + list);
Integer[] num = { 2, 4, 7, 5, 9 };
  
        // using [Link]() to find minimum element
        // using only 1 line.
        int min = [Link]([Link](num));
  
        // using [Link]() to find maximum element
        // using only 1 line.
        int max = [Link]([Link](num));

Hackerrank Time conversion


public static void main(String[] args) {

Scanner scan = new Scanner([Link]);


String time = [Link]();
String tArr[] = [Link](":");
String AmPm = tArr[2].substring(2,4);
int hh,mm,ss;
hh = [Link](tArr[0]);
mm = [Link](tArr[1]);
ss = [Link](tArr[2].substring(0,2));

String checkPM = "PM",checkAM ="AM";


int h = hh;
if([Link](checkAM) && hh==12)
h=0;
else if([Link](checkPM)&& hh<12)
h+=12;

[Link]("%02d:%02d:%02d",h,mm,ss);
}

// Modify array to make each 'i' contain the running sum


of prior elements
for (int i = 1; i < n; i++) {
sum[i] += sum[i - 1];
}
for (int i = m; i < n; i++) {
// If the sum of the piece is equal to 'd'
if (sum[i] - sum[i - m] == d) {
// Increment ways counter
numberOfWays++;
}
}
 >> right shift
class Test {
    public static void main(String args[])  {
       int x = -4;
       [Link](x>>1);   
       int y = 4;
       [Link](y>>1);   
    }    
}
Output:

-2
2

Sock Merchant HR
mport [Link].*;

class Solution {

public static void main(String[] args) {


Scanner scan = new Scanner([Link]);
int n = [Link]();
HashMap<Integer, Integer> colors = new HashMap<Integer, Integer>();

while(n-- > 0) {
int c = [Link]();
Integer frequency = [Link](c);

// If new color, add to map


if(frequency == null) {
[Link](c, 1);
}
else { // Increment frequency of existing color
[Link](c, frequency + 1);
}
}
[Link]();

// Count and print the number of pairs


int pairs = 0;
for(Integer frequency : [Link]()) {
pairs += frequency >> 1;
}
[Link](pairs);
}
}

You might also like