我试过了:
ArrayList<Pelicula> peliculas = YIFY.obtenerPeliculasPorVenir();
Run Code Online (Sandbox Code Playgroud)
是#obtenerPeliculasPorVenir:
public static List<Pelicula> obtenerPeliculasPorVenir(){
List peliculas = null;
try {
peliculas = mapper.readValue(new API().peticionTexto("http://yts.re/api/upcoming.json"), new TypeReference<List<Pelicula>>(){});
}
catch (IOException excepcion) {
System.out.println(excepcion.getMessage());
}
return peliculas;
}
}
Run Code Online (Sandbox Code Playgroud)
如果ArrayList 实现 List为什么我不能这样做?
是铸造唯一的解决方案还是我应该采取另一种OOP方法?
我见过人们用Map而不是HashMap来声明一个HashMap.
例如Map mapName = new HashMap(); 而我通常使用HashMap mapName = new HashMap(); ;
对于aa HashSet来说,我见过Set setName = new HashSet() ; 使用,而我通常使用HashSet setName = new HashSet()
对于ArrayList(List)等也是如此.
我的问题是,这样做有什么好处,还是仅仅是一个观点?
在这里,我想从.xlsx文件中提取数据,为此我已经添加了poi jar并创建了引用fileInputStream
package demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class DemoExcel {
public static void main(String[] args) throws Exception{
File excel = new File("C:\\Users\\Devaditya\\Documents\\Book1.xlsx");
FileInputStream fis = null;
fis = new FileInputStream(excel);
System.out.println(fis.toString());
HSSFWorkbook wb = new HSSFWorkbook(fis);
System.out.println(wb.toString());
HSSFSheet sh = wb.getSheet("Data");
System.out.println(sh.toString());
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里得到错误: -
Exception in thread "main" java.io.IOException: Invalid header signature; read 0, expected -2226271756974174256
at org.apache.poi.poifs.storage.HeaderBlockReader.<init>(HeaderBlockReader.java:88)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:83)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:210)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:191)
at …Run Code Online (Sandbox Code Playgroud) 在之前的问题得到解决的基础上,但它导致了另一个问题.如果协议/类类型存储在集合中,则检索并实例化它们会引发错误.一个假设的例子如下.范例基于"程序到接口而非实现""编程到接口" 是什么意思?
public protocol ISpeakable {
init()
func speak()
}
class Cat : ISpeakable {
required init() {}
func speak() {
println("Meow");
}
}
class Dog : ISpeakable {
required init() {}
func speak() {
println("Woof");
}
}
//Test class is not aware of the specific implementations of ISpeakable at compile time
class Test {
func instantiateAndCallSpeak<T: ISpeakable>(Animal:T.Type) {
let animal = Animal()
animal.speak()
}
}
// Users of the Test class are aware of the specific implementations at …Run Code Online (Sandbox Code Playgroud) 任何想要访问java.util.ArrayList设施的用户都必须遵守提供的使用合同java.util.List.
但是人们可以轻松地破坏这个使用合同并访问这些方法 trimToSize
public void trimToSize() { ... }
Run Code Online (Sandbox Code Playgroud)
和 ensureCapacity
public void ensureCapacity(int minCapacity) { ...}
Run Code Online (Sandbox Code Playgroud)
因为这两种方法既不会被覆盖java.util.AbstractList也不会被实现java.util.List.
那么,为什么这两个方法提供了public级别访问并打破了提供的抽象java.util.List?
我正在尝试在名为的ArrayList上执行以下流操作parts:
parts
.stream()
.map(t -> t.toLowerCase())
.distinct()
.sorted()
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
其中parts包含这样的字符串:
Adventures
in
Disneyland
Two
blondes
were
going
to
Disneyland
....
Run Code Online (Sandbox Code Playgroud)
除了查看调试器之外,parts根本没有改变.不确定我是否错过了这个过程的一些步骤?
在Java中,如何覆盖继承类中变量的类类型?例如:
class Parent {
protected Object results;
public Object getResults() { ... }
}
class Child extends parent {
public void operation() {
... need to work on results as a HashMap
... results.put(resultKey, resultValue);
... I know it is possible to cast to HashMap everytime, but is there a better way?
}
public HashMap getResults() {
return results;
}
Run Code Online (Sandbox Code Playgroud) ArrayList<String> stock_list = new ArrayList<String>();
stock_list.add("stock1");
stock_list.add("stock2");
Run Code Online (Sandbox Code Playgroud)
I have this ArrayList, how do I separate into separate Strings so that I get String a="stock1"; String b="stock2"?
问题:为什么可以声明抽象类的变量,然后将该变量设置为null,然后无法访问任何方法.这个抽象类概念背后的原因是什么?
类:
package ReadWriteFile;
public abstract class GraphicObject {
int home = 100;
final int score = 0;
abstract void draw();
abstract String meMethod1();
abstract void meMethod2();
void meMethod3() {
System.out.println("test");;
}
public static void main(String[] i){
GraphicObject o = null;
o.meMethod3();
}
Run Code Online (Sandbox Code Playgroud)
}
我开始认为我不像我想的那样理解多态性.
我有以下情况:
public class Testing {
public static void main(String[] args){
interTest myTest = new classTest();
myTest.classMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
使用给定的界面:
public interface interTest {
public boolean checkBoolean();
public void method();
}
Run Code Online (Sandbox Code Playgroud)
然后是具体的课程:
public class classTest implements interTest{
public classTest() {
// TODO Auto-generated constructor stub
}
public void classMethod(){
System.out.println("fail");
}
// Both method() and checkBoolean() are overridden here & do nothing.
}
}
Run Code Online (Sandbox Code Playgroud)
Oracle文档演示了如何实现接口然后添加其他方法,甚至实现多个接口(因此包括不在其中一个接口中的方法),我认为这很常见,直到我遇到试图自己尝试这样做的问题.
在这种情况下,我无法访问,classMethod因为它不在界面内.
The method classMethod() is undefined for the type interTest
Run Code Online (Sandbox Code Playgroud)
我对多态性的理解是什么?我想在表单中声明一个变量:
Interface object …Run Code Online (Sandbox Code Playgroud) java ×9
arraylist ×3
interface ×2
abstraction ×1
apache-poi ×1
collections ×1
java-stream ×1
list ×1
map ×1
methods ×1
polymorphism ×1
scope ×1
swift ×1