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

arraylist solutios

The document presents multiple Java solutions using ArrayLists for managing and displaying items and names. Solutions include creating an ItemType class for item details, adding items to an ArrayList, and searching for names based on user input. Each solution demonstrates different functionalities such as displaying item information, counting occurrences, and accessing elements by index.

Uploaded by

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

arraylist solutios

The document presents multiple Java solutions using ArrayLists for managing and displaying items and names. Solutions include creating an ItemType class for item details, adding items to an ArrayList, and searching for names based on user input. Each solution demonstrates different functionalities such as displaying item information, counting occurrences, and accessing elements by index.

Uploaded by

jetblackaqua957
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

ARRAYLIST SOLN

SOLUTION 1

import java.util.*;
class ItemType
{
private String name;private double deposit;private double costPerDay;
ItemType(String a,double b,double c)
{
this.name=a;
this.deposit=b;
this.costPerDay=c;
}
public String toString()
{
return String.format("%-20s%-20.1f%-20.1f",name,deposit,costPerDay);
}
}
class ArrayListObjectMain
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
int n=Integer.parseInt(sc.nextLine());
ArrayList<ItemType> itemlist=new ArrayList<>();
for (int i=0;i<n;i++)
{
String aa=sc.nextLine();
double bb=Double.parseDouble(sc.nextLine());
double cc=Double.parseDouble(sc.nextLine());
itemlist.add(new ItemType(aa,bb,cc));
}
System.out.printf("%-20s%-20s%-20s","Name","Deposit","Cost Per Day");
System.out.println();
for( ItemType item:itemlist)
{
System.out.println(item);
}
}
}

SOLUTOJN 2

class ArrayListMain
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=Integer.parseInt(sc.nextLine());
ArrayList<String> item= new ArrayList<>();
for (int i=0;i<n;i++)
{
String aa=sc.nextLine();
item.add(new String(aa));
}
for(String name:item)
{
System.out.println(name);
}}}

SOLUTION 3

public ItemType(String name, Double deposit, Double costPerDay)


{
this.name=name;
this.deposit=deposit;
this.costPerDay=costPerDay;
}
public String toString()
{
return String.format("%-20s%-20.1f%-20.1f",name,deposit,costPerDay);
}
}

class ArrayListObjectMain
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=Integer.parseInt(sc.nextLine());
ArrayList<ItemType> items=new ArrayList<>();
for(int i=0;i<n;i++)
{
String aa=sc.nextLine();
Double bb=Double.parseDouble(sc.nextLine());
Double cc=Double.parseDouble(sc.nextLine());
items.add(new ItemType(aa,bb,cc));
}

SOLUTION 4
import java.util.*;
class AMin
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();sc.nextLine();
ArrayList<String> names=new ArrayList<>();
for(int i=0;i<n;i++)
{
String a=sc.nextLine();
names.add(a);
}
String r=sc.nextLine();
boolean f=false;int k=0;
for(int j=0;j<n;j++)
{
if(names.get(j).equals(r))
{
f=true;k++;
}
}
System.out.println(f? k:"0");
}
}

SOLUTION 5

class Main
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
int n=Integer.parseInt(sc.nextLine());
ArrayList<String> names= new ArrayList<>();
for(int i=0;i<n;i++)
{
String aa=sc.nextLine();
names.add(new String(aa));
}
int r=Integer.parseInt(sc.nextLine());
if(r<0||r>n-1)
{
System.out.println("Invalid index");
}
else
{
System.out.println("Element at index "+r+": "+names.get(r));
}
}}

You might also like