我正在用Java编写神经网络,而我在使用ArrayList.get()时没有返回变量.
double train(ArrayList<Double> inputVector, double desiredOutput) {
double result = output(inputVector);
double error = desiredOutput - result;
double delta = learningRate_ * error * result * (1.0 - result);
for (int i = 0; i < outputLayer_.weights_.size(); i++) {
outputLayer_.weights_.get(i).doubleValue() += delta * lastOutput_.get(i);
}
ArrayList<Double> hiddenDelta = new ArrayList<Double>();
for (int j = 0; j < hiddenLayer_.size(); j++) {
double HiddenDelta = delta * outputLayer_.weights_.get(j + 1) * lastOutput_.get(j + 1) * (1 - lastOutput_.get(j + 1));
for (int …Run Code Online (Sandbox Code Playgroud) 我正在玩java 1.8.我读到新的集合api工作得更快,因为它在并行化的数据结构上运行操作.
我想看看这个efford.所以我写了下面的代码:
public static void main(String[] args) {
ArrayList<SamplePerson> persons = new ArrayList<>();
for (long i = 0; i < 10000000; i++) {
persons.add(new SamplePerson(EyeColour.BLUE,20,"Max Musterman",Gender.MALE));
}
long nsBefore = System.nanoTime();
// using the new collection api - parallel way??
persons.forEach(samplePerson -> samplePerson.setAge(22));
// sequential way
for(int i = 0; i < persons.size(); i++){
persons.get(i).setAge(22);
}
long nsAfter = System.nanoTime();
long runtime = nsAfter - nsBefore;
System.out.println("Time in Nanoseconds: " + runtime);
}
Run Code Online (Sandbox Code Playgroud)
我的处理器:i7-2600 CPU
使用"并行"方式的结果:以纳秒为单位的时间:74836825
使用顺序方式的结果:以纳秒为单位的时间:45071315
大家可以解释一下这个结果吗?建立这种威胁的开销是否很高?我有点困惑请帮帮我:-)
我正在从我的教科书中复制一个例子,但它拒绝编译.我在某个地方打错了吗?出于某种原因,在客户端代码上,Collections.sort(words)不允许程序编译.任何帮助表示赞赏.代码复制自Stuart Reges和Marty Stepp的"构建Java程序"第2版.我试图通过复制来理解它.
该程序应该创建一个CalendarDate对象以放入ArrayList.通过实现CalendarDate的Comparable接口,我可以使用Collections.sort按顺序在该arraylist中对生日进行排序.但是,这不起作用b/c Collections.sort(日期)将无法运行.
客户端代码(包含问题):
import java.util.*;
// Short program that creates a list of birthdays of the
// first 5 U.S. Presidents and that puts them into sorted order.
// We can now use Collections.sort for ArrayList<CalendarDate> b/c CalendarDate implements the Comparable interface.
public class CalendarDateTest {
public static void main(String[] args) {
ArrayList<CalendarDate> dates = new ArrayList<CalendarDate>(); // Creates a new ArrayList of 'CalendarDate' object type.
// adds a new CalendarDate object with month = 2 and …Run Code Online (Sandbox Code Playgroud) 有没有办法检查ArrayList是否包含对象数组?例如,我有一个arrayList
ArrayList<Integer[]> myList = new ArrayList<Integer[]>();
mylist.add(new Integer[]{5,5});
mylist.add(new Integer[]{1,1});
Run Code Online (Sandbox Code Playgroud)
我想检查它是否包含特定的Integer数组.喜欢:
Integer[] myArray = new Integer[]{5,5};
Run Code Online (Sandbox Code Playgroud)
如果我使用myList.containts(myArray); 它应该返回true,但它检查Integer对象是否被包含,而不是数组值.
有没有办法检查值?
假设我有这样一堂课:
class C
{
int a, b;
ArrayList<int> c = new ArrayList();
};
Run Code Online (Sandbox Code Playgroud)
和这样的集合:
ArrayList<ArrayList<C>> MyList = new ArrayList<ArrayList<C>>();
Run Code Online (Sandbox Code Playgroud)
调用MyList.clear()会取消对MyList中每个ArrayList的所有引用以及MyList中每个ArrayList内的任何其他对象吗?那么,更准确地说,Cc类中的所有元素都将被取消以及Ca和Cb吗?
我有一个小问题,我的arraylist中的元素没有删除.这是一个ArrayList.这是我的代码:
package net.lucrecious.armorconstruct.helpers;
import java.util.ArrayList;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
public class WorldHelper {
public static void replace_coord_with_block(World world, ArrayList<int[]> coords, Block result_block){
for (int[] c : coords){
world.setBlock(c[0], c[1], c[2], result_block);
}
}
public static ArrayList<int[]> get_blocks_in_sphere(EntityPlayer player, World world, int radius, Class<?>...block_types){
ArrayList<int[]> coord_list = new ArrayList<int[]>();
int r = radius;
int i = MathHelper.floor_double(player.posX);
int j = MathHelper.floor_double(player.posY);
int k = MathHelper.floor_double(player.posZ);
for(int x = -r; x < r; x++){
for(int y = -r; …Run Code Online (Sandbox Code Playgroud) 我想用数组实现一个类.如果数组的大小被破坏,我会调用resize()函数.但是我似乎无法将动态数组编码为数据成员.有人可以指导我如何解决这个问题吗?
这是我到目前为止所得到的:
class ArrayList
{
public:
ArrayList()
{
array[ARR_SIZE] = {0};
space = 0;
size = ARR_SIZE;
}
void insert(int place, int value)
{
if (place >= size)
cout << "Sorry, that index is not accessible" << endl;
else
{
array[place] = value;
space++;
if(space == size)
allocate();
}
}
void remove(int place)
{
if(place >= size)
cout << "Sorry, that index is not accessible" << endl;
else
{
array[place] = NULL;
space--;
}
}
void allocate()
{
int* array …Run Code Online (Sandbox Code Playgroud) 如果我有方法
public ArrayList<String> necessaryChanges(Set<Object> setToCheck ) {
//checks to see whether each element of set is correct by comparing it to something
// and if it needs to be changed its added to an arraylist to be returned to the user
}
Run Code Online (Sandbox Code Playgroud)
我需要返回一个包含必要更改的列表,这很好.
但我也想修改setToCheck的变化.我知道你不能在一个方法中返回两个对象.我要问的是这样做最有效的方法是什么.
我知道我可以在主类中创建另一个方法并调用它来更改集合,但它似乎效率很低.
有没有更好的方法来做到这一点.
谢谢
为什么在我自己的容器中添加字符串比在字符串中添加字符串效率低得多ArrayList<String>?
我不知道ArrayList泛型类是如何实现的,但我无法理解为什么类的add方法ArrayList比我的add方法快得多.
这是我的简单容器类:
public class MyContainer
{
private String[] _array;
private int _length = 0;
public MyContainer(int length)
{
if(length < 0) throw new NegativeArraySizeException();
else _length = length;
_array = new String[length];
}
//This is not an efficient add method, but I wouldn't know how to implement
//it otherwise in Java
public void add(String newElement)
{
++_length;
String[] tmp = new String[_length];
for(int i = 0; i < _array.length; …Run Code Online (Sandbox Code Playgroud) 所以,简单的问题似乎令我感到困惑.我有一系列ArrayList因为它们需要可扩展.现在,我希望能够通过简单的for循环从每个对象中拉出第n个对象.
public ArrayList<String> name = new ArrayList<String>();
public ArrayList<Integer> stamina = new ArrayList<Integer>();
public ArrayList<Integer> heart = new ArrayList<Integer>();
public ArrayList<Integer> intel = new ArrayList<Integer>();
public ArrayList<Integer> speed = new ArrayList<Integer>();
Run Code Online (Sandbox Code Playgroud)
我将在稍后的代码中添加/提取这些信息,因此我希望通过单个2D数组可以轻松访问它.有点像,
racerInfo[][] = { name, stamina, heart, intel, speed };
Run Code Online (Sandbox Code Playgroud)
然后,如果我想要赛车编号为7的信息
for ( int i=0; i<=racerInfo.length - 1; i++ ) {
System.out.println(racerInfo[i][7]);
}
Run Code Online (Sandbox Code Playgroud)
我该如何设置racerInfo场地?我似乎无法弄清楚正确的设置.如果你有任何更好的建议,我完全开放,因为我仍然是新的,只是试图拼凑一些有效的东西.
arraylist ×10
java ×9
java-8 ×2
performance ×2
arrays ×1
c++ ×1
class ×1
collections ×1
comparable ×1
containers ×1
interface ×1
list ×1
removeall ×1
set ×1
sorting ×1