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踩坑的更多相关文章

  1. 『OGG 02』Win7 配置 Oracle GoldenGate Adapter Java 踩坑指南

    上一文章 <__Win7 配置OGG(Oracle GoldenGate).docx>定下了 两个目标: 目标1: 给安装的Oracle_11g 创建 两个用户 admin 和 root ...

  2. Java踩坑之路

    陆陆续续学Java也快一年多了,从开始的一窍不通到现在的初窥门径,我努力过,迷茫过,痛过,乐过,反思过,沉淀过.趁着新年,我希望能把这些东西记下来,就当是我一路走来的脚印. 一.初识网站应用 记得第一 ...

  3. JAVA踩坑录

    以前踩了很多坑,大多忘了.现在踩了坑,想起了一定记下来. 1. 字符串分割,这种工具类,首次使用一定要先看一眼,不然跳坑 commons-lang StringUtils.split分割时会去掉空串: ...

  4. java踩坑记

    1.String 相等 稍微有点经验的程序员都会用equals比较而不是用 ==,但用equals就真的安全了吗,看下面的代码 user.getName().equals("xiaoming ...

  5. mongo java 踩坑记

    为什么会有这么多坑 1.  Java会把 id:String = "合法ObjectId"  好心好意的 转为  _id:ObjectId 类型. 2. 为了避免第1点, 我定义了 ...

  6. Java踩坑记系列之Arrays.AsList

    java.util.Arrays的asList方法可以方便的将数组转化为集合,我们平时开发在初始化ArrayList时使用的比较多,可以简化代码,但这个静态方法asList()有几个坑需要注意: 一. ...

  7. Spark SQL 用户自定义函数UDF、用户自定义聚合函数UDAF 教程(Java踩坑教学版)

    在Spark中,也支持Hive中的自定义函数.自定义函数大致可以分为三种: UDF(User-Defined-Function),即最基本的自定义函数,类似to_char,to_date等 UDAF( ...

  8. Java踩坑之List的removeAll方法

    最近在公司写东西,发现List的removeAll方法报错 Demo代码如下: List<Long> ids1 = Arrays.asList(1L, 3L, 2L); List<L ...

  9. Java 开发中如何正确踩坑

    为什么说一个好的员工能顶 100 个普通员工 我们的做法是,要用最好的人.我一直都认为研发本身是很有创造性的,如果人不放松,或不够聪明,都很难做得好.你要找到最好的人,一个好的工程师不是顶10个,是顶 ...

随机推荐

  1. 那些年读过的书《Java并发编程实战》和《Java并发编程的艺术》三、任务执行框架—Executor框架小结

    <Java并发编程实战>和<Java并发编程的艺术>           Executor框架小结 1.在线程中如何执行任务 (1)任务执行目标: 在正常负载情况下,服务器应用 ...

  2. Mysql 通过information_schema爆库,爆表,爆字段

    MySQL版本大于5.0时,有个默认数据库information_schema,里面存放着所有数据库的信息(比如表名. 列名.对应权限等),通过这个数据库,我们就可以跨库查询,爆表爆列. 若要从这些视 ...

  3. es6 学习二 Generator

    安装babel的拓展包(Polyfill) ,对Generator的转义 这是一个补完babel支持es6的拓展包,配置步骤为3个: 打开命令行键入 npm install --save-dev ba ...

  4. vue.cli项目中src目录每个文件夹和文件的用法

    assets文件夹是放静态资源:components是放组件:router是定义路由相关的配置:view视图:app.vue是一个应用主组件:main.js是入口文件:

  5. 第一章 初识windows程序

    window 操作系统中,处处是窗体 简单 强大 方便 灵活 步骤 新建项目 项目类型 visual C#项目 模板 window应用程序 用partial 将同一个窗体的代码分开放在两个文件中: 一 ...

  6. tail命令 输出文件后n行,默认查看文件的后10行

    默认查看文件的后10行 -n 3 数字   也可以忽略-n 直接加数字 tail 3 查看文件后3行 [root@localhost ~]# tail /etc/passwd // 默认查看文件的后十 ...

  7. dbdeployer 快速安装MySQL8.0各测试环境

    Linux系统必须安装有Go语言: 下载最新的包:https://github.com/datacharmer/dbdeployer/releases     解压:  tar -xzf dbdepl ...

  8. 三角形的优雅值(map和哈希表)

    给你 n 个三角形,每个三角形有一个优雅值,然后给出一个询问,每次询问一个三角形,求与询问的三角形,相似的三角形中的优雅值最大是多少.★数据输入第一行输入包括 n 一个数字,接下来 n 行,每行四个整 ...

  9. vue常用插件汇总

    UI-框架element - 饿了么出品的Vue2的web UI工具套件 mint-ui - Vue 2的移动UI元素 iview - 基于 Vuejs 的开源 UI 组件库 Keen-UI - 轻量 ...

  10. linux下automake用法

    linux下automake用法 2017年02月06日 09:21:14 阅读数:3684 标签: makemakefilegnulinux   作为Linux下的程序开发人员,大家一定都遇到过Ma ...