Answers for "generic programming in java"

0

generic argument java

static void fromArrayToCollection(Object[] a, Collection<?> c) {
    for (Object o : a) { 
        c.add(o); // compile-time error
    }
}
Posted by: Guest on December-20-2020
0

generic argument java

static void fromArrayToCollection(Object[] a, Collection<?> c) {
    for (Object o : a) { 
        c.add(o); // compile-time error
    }
}
Posted by: Guest on December-20-2020
-1

generics in java

import java.util.Scanner;
import reader.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        String filePath = scanner.nextLine().trim();
        System.out.println("The file path is " + filePath);
        String type = filePath.substring(filePath.lastIndexOf('.') + 1).replaceAll("\\W", "");
        System.out.println("The reader type is " + type);
        AbstractReaderService reader = getReaderService(type);
        // reader.read(filePath);
        // reader.printContent();

    }

    private static AbstractReaderService getReaderService (String type) throws Exceptions.TypeNotFoundException {
        switch (type) {
            case "txt": return new TxtReaderService();
            case "json": return new JsonReaderService();
            default: throw new Exceptions().new TypeNotFoundException("There is no \"" + type + "\" type of reader.");
        }
    }
}
Posted by: Guest on August-10-2021
-1

generics in java

import java.util.Scanner;
import reader.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        String filePath = scanner.nextLine().trim();
        System.out.println("The file path is " + filePath);
        String type = filePath.substring(filePath.lastIndexOf('.') + 1).replaceAll("\\W", "");
        System.out.println("The reader type is " + type);
        AbstractReaderService reader = getReaderService(type);
        // reader.read(filePath);
        // reader.printContent();

    }

    private static AbstractReaderService getReaderService (String type) throws Exceptions.TypeNotFoundException {
        switch (type) {
            case "txt": return new TxtReaderService();
            case "json": return new JsonReaderService();
            default: throw new Exceptions().new TypeNotFoundException("There is no \"" + type + "\" type of reader.");
        }
    }
}
Posted by: Guest on August-10-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language