《Cracking the Coding Interview》——第14章:Java——题目4
2014-04-26 19:02
题目:解释下C++里模板和java里泛型的区别?
解法:我很少用java,属于连语法都不过关的程度。所以这个题还真没法详细答,查了些资料以后写了以下几点。
代码:
// 14.4 tell me about the differences between C++ template and java generics.
// Answer:
// 1. C++ template can be used on built-in type and user-defined types, java generics can only be used on classes. Integer for int, Double for double.
// 2. java generics can put some restrictions on the <T>, such as <T extends superclass>, whereas this is not practical in C++.
// 3. You may use downcast instead of generics, but generics enhance the resuability of code, so is template in C++.
import java.util.Vector; public class TestJava<T> {
public T data; public TestJava(T data) {
// TODO Auto-generated constructor stub
this.data = data;
} String getType() {
return this.data.getClass().getName();
} public static void main(String[] args) {
Vector<Integer> v = new Vector<Integer>();
v.add(2);
v.add(1); TestJava<Vector<Integer>> testJava = new TestJava<Vector<Integer>>(v);
System.out.println(testJava.getType());
System.out.println(v);
}
}
《Cracking the Coding Interview》——第14章:Java——题目4的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 《Cracking the Coding Interview》——第14章:Java——题目6
2014-04-26 19:11 题目:设计一个循环数组,使其支持高效率的循环移位.并能够使用foreach的方式访问. 解法:foreach不太清楚,循环移位我倒是实现了一个,用带有偏移量的数组实现 ...
- 《Cracking the Coding Interview》——第14章:Java——题目5
2014-04-26 19:06 题目:Java中的对象反射机制是什么?有鼠么用? 解法:完全不了解,因为java编程经验太少,完全没用过.查了一些资料后,感觉反射机制是个强大并需要边用边体会的强大工 ...
- 《Cracking the Coding Interview》——第14章:Java——题目3
2014-04-26 18:59 题目:final.finally.finalize有什么区别? 解法:烂大街之java语法题.此题被多少公司考过我不知道,反正我确实遇见过一次了. 代码: // 14 ...
随机推荐
- C++ Eigen库和Matlab对比
// A simple quickref for Eigen. Add anything that's missing. // Main author: Keir Mierle #include &l ...
- 【JavaScript 封装库】Prototype 原型版发布!
/* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =============================================== ...
- Android(java)学习笔记68:使用proguard混淆android代码
1. 当前是有些工具比如apktool,dextojar等是可以对我们android安装包进行反编译,获得源码的.为了减少被别人破解,导致源码泄露,程序被别人盗取代码,等等.我们需要对代码进行混淆,a ...
- 设有三个进程A、B、C,其中A与B构成一对生产者与消费者(A为生产者,B为消费者),共享一个由n个缓冲块组成的缓冲池;B与C也构成一对生产者与消费者(此时B为生产者,C为消费者)共享另一个由m个缓冲块组成的缓冲池。用P、V操作描述它们之间的同步关系。
生产者消费者问题 设信号量mutex1, mutex2, full1, full2, empty1, empty2分别表示1和2号缓冲区的访问互斥, 是否满, 是否空 semaphore mutex1 ...
- Laplace算子和Laplacian矩阵
1 Laplace算子的物理意义 Laplace算子的定义为梯度的散度. 在Cartesian坐标系下也可表示为: 或者,它是Hessian矩阵的迹: 以热传导方程为例,因为热流与温度的梯度成正比,那 ...
- CXF报错[1 counts of IllegalAnnotationExceptions]and[Two classes have the same XML type name]and[Use @XmlType.name and @XmlType.namespace to assign different names to them]
启动时CXF报错如下: Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalA ...
- jQuery对表单、表格的操作以及更多应用
表单和表格都是HTML的重要组成部分,分别用于采集.提交用户输入的信息和显示列表数据. 表单应用:一个表单有3个基本组成部分: ①表单标签:包含处理表单数据所用的服务器端程序URL以及数据提交到服务器 ...
- .net core自定义特性操作
最近移植之前写的几个类,发现特性操作发生了一些改变. 直接看代码,建立表和字段特性类,添加一个用户表,设置好特性. using System; namespace TestDemo { /// < ...
- 在线代码编辑器 Codemirror 的轻量级 React 组件
代码编辑器 CodeMirror 的轻量级 React 组件 demo @uiw-react.github.io/react-codemirror/ 特性:
- sql的使用
1.自动获取最新订单号 select concat('XJDD',DATE_FORMAT(now(),'%Y%m%d'), LPAD(( FOR )) , max(SUBSTRING(inquiryn ...