package priceton;

import java.io.IOException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class RecipesCyclicBarrier {
public static CyclicBarrier barrier = new CyclicBarrier(3); public static void main(String[] args) throws IOException, InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(3);
executor.submit(new Thread(new Runner("NO-1")));
executor.submit(new Thread(new Runner("NO-2")));
executor.submit(new Thread(new Runner("NO-3")));
executor.shutdown();
}
} class Runner implements Runnable {
private String name; public Runner(String name) {
this.name = name;
} public void run() {
System.out.println(name + " READY!");
try {
RecipesCyclicBarrier.barrier.await();
} catch (Exception e) {
}
System.out.println(name + " GO!");
} }

  

package priceton;
import java.util.*;
public class ArrayListTest {
public static void main(String[] args) {
List<String> list=new ArrayList<String>();
list.add("Hello");
list.add("World");
list.add("HAHAHAHA");
//第一种遍历方法使用foreach遍历List
for (String str : list) { //也可以改写for(int i=0;i<list.size();i++)这种形式
System.out.println(str);
} //第二种遍历,把链表变为数组相关的内容进行遍历
String[] strArray=new String[list.size()];
list.toArray(strArray);
for(int i=0;i<strArray.length;i++) //这里也可以改写为 foreach(String str:strArray)这种形式
{
System.out.println(strArray[i]);
} //第三种遍历 使用迭代器进行相关遍历 Iterator<String> ite=list.iterator();
while(ite.hasNext())//判断下一个元素之后有值
{
System.out.println(ite.next());
}
}
}

  

Java菜鸟教程 面向对象(二)——constructor、overload与一个练习 - CSDN博客 https://blog.csdn.net/thomasli2017/article/details/77338454

Java 对象和类 | 菜鸟教程 http://www.runoob.com/java/java-object-classes.html

Java中类的构造方法 - CSDN博客 https://blog.csdn.net/zw1996/article/details/52878270

Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects) https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

Providing Constructors for Your Classes

【使用类名。没有返回】

A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations—except that they use the name of the class and have no return type. For example, Bicycle has one constructor:

public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
To create a new Bicycle object called myBike, a constructor is called by the new operator:

Bicycle myBike = new Bicycle(30, 0, 8);
new Bicycle(30, 0, 8) creates space in memory for the object and initializes its fields.

Although Bicycle only has one constructor, it could have others, including a no-argument constructor:

public Bicycle() {
gear = 1;
cadence = 10;
speed = 0;
}
Bicycle yourBike = new Bicycle(); invokes the no-argument constructor to create a new Bicycle object called yourBike.

Both constructors could have been declared in Bicycle because they have different argument lists. As with methods, the Java platform differentiates constructors on the basis of the number of arguments in the list and their types. You cannot write two constructors that have the same number and type of arguments for the same class, because the platform would not be able to tell them apart. Doing so causes a compile-time error.

You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass. In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does. If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor.

You can use a superclass constructor yourself. The MountainBike class at the beginning of this lesson did just that. This will be discussed later, in the lesson on interfaces and inheritance.

You can use access modifiers in a constructor's declaration to control which other classes can call the constructor.

https://mp.weixin.qq.com/s/0W7-wAwonXLtV4D0uQBuxQ

ArrayList 的实现原理,如何测试 ArrayList 动态分配内存带来的内存、CPU 变化?

  • ArrayList 是用数组实现的,这个数组在内存中是连续的

  • 索引 ArrayList 时,速度比原生数组慢是因为你要用 get 方法,这是一个函数调用,而数组直接用下标访问,相当于直接操作内存地址,速度当然非常快

  • 新建 ArrayList 的时候,JVM 为其分配一个默认或指定大小的连续内存区域

  • 每次增加元素会检测容量,不足则创建新的连续内存区域,并将原来的内存区域数据复制到新的内存区域,然后再用 ArrayList 中引用原来封装的数组对象的引用变量引用到新的数组对象。

When to use ArrayList and LinkedList in Java - javatpoint https://www.javatpoint.com/when-to-use-arraylist-and-linkedlist-in-java

When to use ArrayList and LinkedList in Java

ArrayList provides constant time for search operation, so it is better to use ArrayList if searching is more frequent operation than add and remove operation. The LinkedList provides constant time for add and remove operations. So it is better to use LinkedList for manipulation.

ArrayList has O(1) time complexity to access elements via the get and set methods.

LinkedList has O(n/2) time complexity to access the elements.

LinkedLinked class implements Deque interface also, so you can get the functionality of double ended queue in LinkedList. The ArrayList class doesn't implement Deque interface.

In sort, ArrayList is better to access data wherease LinkedList is better to manipulate data. Both classes implements List interface.

package com.mycom;

import java.util.ArrayList;
import java.util.List; public class ArrayListExample {
public static void main(String[] args) {
//Array is better to store and view data
List<String> list = new ArrayList<>();
list.add("ankit");
list.add("peter");
list.add("mayank"); System.out.println("Traversing ArrayList...");
for (String s : list) {
System.out.println(s);
}
}
}

  

Traversing ArrayList...
ankit
peter
mayank

package com.mycom;

import java.util.LinkedList;
import java.util.List; public class LinkedListExample {
public static void main(String[] args) {
//Linkedlist is better to manipulate data
List<String> list = new LinkedList<>();
list.add("ankit");
list.add("peter");
list.add("mayank");
System.out.println("After adding:" + list);
list.remove("peter");
System.out.println("After removing:" + list); /**
* Replaces the element at the specified position in this list with the
* specified element.
*
* @param index index of the element to replace
* @param element element to be stored at the specified position
* @return the element previously at the specified position
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
list.set(1, "vivek");
System.out.println("After changing:" + list);
}
}

  

After adding:[ankit, peter, mayank]
After removing:[ankit, mayank]
After changing:[ankit, vivek]

Java ArrayList和Vector、LinkedList与ArrayList、数组(Array)和列表集合(ArrayList)的区别 - 随行-LV - 博客园 https://www.cnblogs.com/Liang-Haishan216/p/6186920.html

ArrayList vs LinkedList in Java - GeeksforGeeks https://www.geeksforgeeks.org/arraylist-vs-linkedlist-java/

ArrayList vs LinkedList in Java

Two popular lists in Java are:

1. ArrayList:-Implemented with the concept of dynamic array.

ArrayList<Type> arrL = new ArrayList<Type>();

Here Type is the data type of elements in ArrayList
to be created

2. LinkedList:-Implemented with the concept of doubly linked list.

LinkedList<Type> linkL = new LinkedList<Type>();

Here Type is the data type of elements in LinkedList
to be created

Comparision between ArrayListand LinkedList:-

  1. Insertions are easy and fast in LinkedList as compared to ArrayList because there is no
    risk of resizing array and copying content to new array if array gets full which makes
    adding into ArrayList of O(n) in worst case, while adding is O(1) operation in LinkedList
    in Java. ArrayList also needs to be update its index if you insert something anywhere except
    at the end of array.
  2. Removal also better in LinkedList than ArrayList due to same reasons as insertion.
  3. LinkedList has more memory overhead than ArrayList because in ArrayList each index only
    holds actual object (data) but in case of LinkedList each node holds both data and address
    of next and previous node.
  4. Both LinkedList and ArrayList require O(n) time to find if an element is present or not. However we can do Binary Search on ArrayList if it is sorted and therefore can search in O(Log n) time.
// Java program to demonstrate difference between ArrayList and
// LinkedList.
package com.mycom;

import java.util.ArrayList;
import java.util.LinkedList; public class ArrayListLinkedListExample {
public static void main(String[] args) {
ArrayList<String> arrlistobj = new ArrayList<>();
arrlistobj.add("0.Practice");
arrlistobj.add("1.Quiz");
arrlistobj.add("2.Code");
arrlistobj.remove(1);//Remove value at index 2
System.out.println("ArrayList object output:" + arrlistobj);
// Checking if an elemant is present.
if (arrlistobj.contains("2.Code"))
System.out.println("Found");
else
System.out.println("Not found"); LinkedList llobj = new LinkedList();
llobj.add("0.Practice");
llobj.add("1.Quiz");
llobj.add("2.Code");
llobj.remove("1.Quiz");
System.out.println("LinkedList object output:" + llobj);
//Checking if an element is present
if (llobj.contains("2.Code"))
System.out.println("Found");
else
System.out.println("Not found");
}
}

迭代器遍历列表 构造方法 constructor ArrayList Vector LinkedList Array List 时间复杂度的更多相关文章

  1. ArrayList Vector LinkedList(一)

    ArrayList Vector LinkedList 区别与用法 ArrayList 和Vector是采用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,都允许直接序号索引元素, ...

  2. 请说出ArrayList,Vector, LinkedList的存储性能和特性

    请说出ArrayList,Vector, LinkedList的存储性能和特性 解答:ArrayList和Vector都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都 ...

  3. paip.提升性能---list,arraylist,vector,linkedlist,map的选用..

    paip.提升性能---list,arraylist,vector,linkedlist,map的选用.. arraylist,vector基本一样,但是,vector线程安全的. 作者Attilax ...

  4. Arraylist Vector Linkedlist区别和用法 (转)

    ArrayList 和Vector是采用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,都允许直接序号索引元素,但是插入数据要设计到数组元素移动等内存操作,所以索引数据快插入数据慢 ...

  5. java集合(ArrayList,Vector,LinkedList,HashSet,TreeSet的功能详解)

    说起集合,我们会潜意识里想到另外一个与之相近的名词——数组,OK!两者确实有相似之处,但也正是这点才是我们应该注意的地方,下面简单列出了两者的区别(具体功能的不同学习这篇文章后就会明白了): 数组 长 ...

  6. Java中ArrayList,Vector,LinkedList,HashMap,HashTable,HashSet对比及总结

    1.所有的集合的父类都是Collection的接口 2.Set List Map 区别 A  在Set里面:无法添加元素的顺序,所以Set里面的元素不能重复 B  在List中:有索引号,类似于数组, ...

  7. ArrayList Vector LinkedList 区别与用法

    转载自: http://www.cnblogs.com/mgod/archive/2007/08/05/844011.html 最近用到了,所以依然是转载 ArrayList 和Vector是采用数组 ...

  8. Java ArrayList Vector LinkedList Stack Hashtable等的差别与用法(转)

    ArrayList 和Vector是采取数组体式格式存储数据,此数组元素数大于实际存储的数据以便增长和插入元素,都容许直接序号索引元素,然则插入数据要设计到数组元素移动等内存操纵,所以索引数据快插入数 ...

  9. 【转】ARRAYLIST VECTOR LINKEDLIST 区别与用法

    转自:http://www.cnblogs.com/mgod/archive/2007/08/05/844011.html ArrayList 和Vector是采用数组方式存储数据,此数组元素数大于实 ...

随机推荐

  1. Python 入门之 递归

    Python 入门之 递归 1.递归: 递:一直传参 归:返回 (1)不断调用自己本身(无效递归 -- 死递归) def func(): print(1) func() func() (2)有明确的终 ...

  2. Django 调试models 输出的SQL语句 定位查看结果

    django 调试models变得更为简单了,不用像之前的版本, 手工去调用django query, 才能打印出之前的代码是执行的什么SQL语句. 1.3开始只需在settings.py里,配置如下 ...

  3. 通过编写串口助手工具学习MFC过程——(二)通过“打开串口”按钮了解基本操作

    通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...

  4. Zookeeper入门概要

    ZooKeeper是一个开源的分布式协调服务,由雅虎创建,是Google Chubby的开源实现.ZooKeeper的设计目标是将那些复杂且容易出错的分布式一致性服务封装起来,构成一个高效可靠的原语集 ...

  5. webpack output的path publicPath

    path是用来定义入口文件保存的地址,而publicPath是定义非入口文件需要抽离保存的第三方文件的保存地址 vue-cli 中HtmlWebpackPlugin生成html,都会存在path路径上 ...

  6. linux Nginx 的安装

    确保安装了 gcc,openssl-devel,pcre-devel,zilb-devel 下载官网:http://nginx.org/ [root@localhost tools]# wget ht ...

  7. tr 替换或删除字符

    1.命令功能 tr 从标准输入中替换,压缩间隔或者删除字符并从定向到标准输出. 2.语法格式 tr  option  SET1  SET2 参数 参数说明 -c 取代所有SET1中字符串 -d 删除所 ...

  8. Linux openssh8.0p1升级步骤(shell版本)

    运维自动化时代,手动升级太徒劳了,为了提高效率及准确率,自动化安装是必备的. 下面是通过shell写的脚本.也可以将其应用到ansible上. 准备好安装文件: openssh-8.0p1.tar.g ...

  9. word2007导出pdf带书签

    1.关闭所有word文档 2.下载Word_2007_SaveAsPDFandXPS_12.0_XiaZaiBa.exe安装 3.如果出错请重启PC

  10. adam优化

    AdaGrad (Adaptive Gradient,自适应梯度) 对每个不同的参数调整不同的学习率, 对频繁变化的参数以更小的步长进行更新,而稀疏的参数以更大的步长进行更新. gt表示第t时间步的梯 ...