java踩坑
1. java判断两个字符串是否相等用equals
2. java只传递指针遇到的坑:
1 import java.util.*;
2
3 public class mapTest {
4 public static class EntryWeight{
5 public int entryid;
6 public float wordweight;
7 public int getentryid(){return entryid;}
8 public float getwordweigth(){return wordweight;}
9 public String toString() {
10 return "id=" + entryid + ", weight=" + wordweight;
11 }
12 }
13 public static void main(String[] args) {
14 ArrayList<EntryWeight> entryPairs = new ArrayList<EntryWeight>(5);
15 for(int i=0;i<5;i++){
16 EntryWeight entryPair = new EntryWeight();
17 entryPair.entryid = i;
18 entryPair.wordweight = (float)0.5+i;
19 entryPairs.add(entryPair);
20 System.out.println("entry pair: " + entryPair.getentryid() );
21
22 }
23 for(int i=0;i<entryPairs.size();i++){
24 System.out.println("entrys "+i+" : " + entryPairs.get(i).entryid );
25 }
26 for(int i=0;i<entryPairs.size();i++){
27 System.out.println("entrys "+i+" : " + entryPairs.get(i).wordweight );
28 }
29 for (Iterator<EntryWeight> iterator = entryPairs.iterator(); iterator.hasNext(); ) {
30 System.out.println(iterator.next());
31 }
32 }
33 }
这样写是正确的,输出:
entry pair: 0
entry pair: 1
entry pair: 2
entry pair: 3
entry pair: 4
entrys 0 : 0
entrys 1 : 1
entrys 2 : 2
entrys 3 : 3
entrys 4 : 4
entrys 0 : 0.5
entrys 1 : 1.5
entrys 2 : 2.5
entrys 3 : 3.5
entrys 4 : 4.5
id=0, weight=0.5
id=1, weight=1.5
id=2, weight=2.5
id=3, weight=3.5
id=4, weight=4.5
把EntryWeight entryPair = new EntryWeight()放在for循环外边,其他都不改,
public static void main(String[] args) {
ArrayList<EntryWeight> entryPairs = new ArrayList<EntryWeight>(5);
EntryWeight entryPair = new EntryWeight();
for(int i=0;i<5;i++){
entryPair.entryid = i;
entryPair.wordweight = (float)0.5+i;
entryPairs.add(entryPair);
System.out.println("entry pair: " + entryPair.getentryid() );
}
输出变成了这样:
entry pair: 0
entry pair: 1
entry pair: 2
entry pair: 3
entry pair: 4
entrys 0 : 4
entrys 1 : 4
entrys 2 : 4
entrys 3 : 4
entrys 4 : 4
entrys 0 : 4.5
entrys 1 : 4.5
entrys 2 : 4.5
entrys 3 : 4.5
entrys 4 : 4.5
id=4, weight=4.5
id=4, weight=4.5
id=4, weight=4.5
id=4, weight=4.5
id=4, weight=4.5
是因为entryPairs.add()只是把地址传过来了,并没有新建一个entryPair,想起早晨师兄还说到这个,这么快就踩到了。
Java中集合提供的拷贝构造函数只支持浅拷贝而不是深拷贝,这意味着存储在原有List和克隆List中的对象会保持一致,并指向Java堆中同一内存地址。
2. map的key如果用数字的话可以定义成:
Map<Integer, EntryWeight> db = new HashMap<Integer, EntryWeight>();
db.put(0, entryList);
java踩坑的更多相关文章
- 『OGG 02』Win7 配置 Oracle GoldenGate Adapter Java 踩坑指南
上一文章 <__Win7 配置OGG(Oracle GoldenGate).docx>定下了 两个目标: 目标1: 给安装的Oracle_11g 创建 两个用户 admin 和 root ...
- Java踩坑之路
陆陆续续学Java也快一年多了,从开始的一窍不通到现在的初窥门径,我努力过,迷茫过,痛过,乐过,反思过,沉淀过.趁着新年,我希望能把这些东西记下来,就当是我一路走来的脚印. 一.初识网站应用 记得第一 ...
- JAVA踩坑录
以前踩了很多坑,大多忘了.现在踩了坑,想起了一定记下来. 1. 字符串分割,这种工具类,首次使用一定要先看一眼,不然跳坑 commons-lang StringUtils.split分割时会去掉空串: ...
- java踩坑记
1.String 相等 稍微有点经验的程序员都会用equals比较而不是用 ==,但用equals就真的安全了吗,看下面的代码 user.getName().equals("xiaoming ...
- mongo java 踩坑记
为什么会有这么多坑 1. Java会把 id:String = "合法ObjectId" 好心好意的 转为 _id:ObjectId 类型. 2. 为了避免第1点, 我定义了 ...
- Java踩坑记系列之Arrays.AsList
java.util.Arrays的asList方法可以方便的将数组转化为集合,我们平时开发在初始化ArrayList时使用的比较多,可以简化代码,但这个静态方法asList()有几个坑需要注意: 一. ...
- Spark SQL 用户自定义函数UDF、用户自定义聚合函数UDAF 教程(Java踩坑教学版)
在Spark中,也支持Hive中的自定义函数.自定义函数大致可以分为三种: UDF(User-Defined-Function),即最基本的自定义函数,类似to_char,to_date等 UDAF( ...
- Java踩坑之List的removeAll方法
最近在公司写东西,发现List的removeAll方法报错 Demo代码如下: List<Long> ids1 = Arrays.asList(1L, 3L, 2L); List<L ...
- Java 开发中如何正确踩坑
为什么说一个好的员工能顶 100 个普通员工 我们的做法是,要用最好的人.我一直都认为研发本身是很有创造性的,如果人不放松,或不够聪明,都很难做得好.你要找到最好的人,一个好的工程师不是顶10个,是顶 ...
随机推荐
- 【python基础】sys
sys模块 参考: https://blog.csdn.net/qq_38526635/article/details/81739321 http://www.cnblogs.com/cherishr ...
- html简单网页1
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Asp.net Mvc action返回多个模型实体给view
1.controller中action代码: public class HomeController : Controller { public ActionResult Detail(int id) ...
- oracle sqlplus login.sql设置
sqlplus在启动时会自动运行两个脚本:glogin.sql.login.sql这两个文件 执行顺序为 1.默认在在$ORACLE_HOME/sqlplus/admin路径下查找glogin.sql ...
- mysql show prifile基本详解
show profile默认情况下,参数处于关闭状态,并保存最近15次的运行结果查看profile是否开启 show variables like '%profi%';开启profile记录功能 se ...
- 010-ThreadGroup线程组
线程组ThreadGroup表示一组线程的集合,一旦一个线程归属到一个线程组之中后,就不能再更换其所在的线程组.那么为什么要使用线程组呢?个人认为有以下的好处:方便统一管理,线程组可以进行复制,快速定 ...
- 004-RestTemplate 使用常见问题
一.使用 同前三节:ClientGetGoodsByGoodsIdResponse response = restTemplate.postForObject(svcUrl, request, Res ...
- 【LDA】周志华
一.书籍
- Ext.define细节分析
自己写的其实还是不懂,再看看别人写的吧Extjs4 源码分析系列一 类的创建过程https://www.cnblogs.com/creazyguagua/p/4302864.htmlhttp://ww ...
- @Value("#{}") 和 @Value("{}")
1 @Value("#{}") SpEL表达式 @Value("#{}") 表示SpEl表达式通常用来获取bean的属性,或者调用bean的某个方法.当然还 ...