javascript

java

python

c#

android

node.js

c++

reactjs

php

jquery

html

css

ios

.net

git

ruby-on-rails

sql

c

string

ruby

如何将元组添加到Swift数组?

我正在尝试将一个元组(例如2项元组)添加到数组中。

var myStringArray: (String,Int)[]? = nil
myStringArray += ("One", 1)

我得到的是:

无法找到接受提供的'+ ='的重载   论点


提示:我尝试对每本参考书进行“ + =”的重载:

@assignment func += (inout left: (String,Int)[], right: (String,Int)[]) {
    left = (left:String+right:String, left:Int+right+Int)
}

...但是没有正确。

有任何想法吗? ...解?

trans by 2020-02-18T19:23:29Z

算法-面试问题:三个数组和O(N * N)

假设我们有三个长度为N的数组,其中包含long类型的任意数字。然后给我们一个数字M(相同类型),我们的任务是从每个数组中选择三个数字A,B和C(换句话说, 应该从第一个数组中选择A,从第二个数组中选择B,从第三个数组中选择C),因此总和A + B + C = M.

问题:我们可以选择所有三个数字并得出O(N2)的时间复杂度吗?


插图:

数组是:

1) 6 5 8 3 9 2
2) 1 9 0 4 6 4
3) 7 8 1 5 4 3

我们得到的M是19那么我们的选择是从第一开始为8,从第二开始为4,从第三开始为7。

trans by 2020-02-18T16:20:03Z

Ruby:如何在出现次数最多的数组中查找项目?

[1, 1, 1, 2, 3].mode
=> 1

['cat', 'dog', 'snake', 'dog'].mode
=> dog
trans by 2020-02-18T16:09:39Z

php-检查数组中的所有值是否均为sam

我需要检查数组中的所有值是否都相同。

例如:

$allValues = array(
    'true',
    'true',
    'true',
);

如果数组中的每个值都等于'true',那么我想回显'all true'。如果数组中的任何值都等于'false',那么我要回声'some false'

关于如何执行此操作的任何想法?

trans by 2020-02-18T07:26:59Z

比较两个数组,忽略Ruby中的元素顺序

我需要检查两个数组是否以任何顺序包含相同的数据。我想使用假想的arr1.sort == arr2.sort936方法来做:

arr1 = [1,2,3,5,4]
arr2 = [3,4,2,1,5]
arr3 = [3,4,2,1,5,5]

arr1.compare(arr2) #true    
arr1.compare(arr3) #false

我使用了arr1.sort == arr2.sort,它似乎可以正常工作,但是有更好的方法吗?

trans by 2020-02-18T05:32:05Z

PHP:快速访问数组值

在php中,我经常需要使用数组来映射变量...但是我似乎无法在一个内衬中做到这一点。 c.f. 例:

// the following results in an error:
echo array('a','b','c')[$key];

// this works, using an unnecessary variable:
$variable = array('a','b','c');
echo $variable[$key];

这是一个小问题,但有时会不时出现错误……我不喜欢这样的事实,我什么都不用使用变量;)

trans by 2020-02-18T02:50:03Z

如何在Python中制作2D数组的副本?

这个问题已经在这里有了答案:

  • 在Python中复制嵌套列表                                     3个答案
  • 如何克隆或复制列表?                                     15个答案

[ [1000, 2], [3, 4] ]是2D阵列。 我希望有一个新变量y=x[:],该变量的值与数组y = x[:][:]的值相同。此外,任何使用Y的进一步操作都不应影响X的值。

在我看来,使用[ [1000, 2], [3, 4] ]很自然,但不适用于数组。 如果我这样做,然后更改y,x也会更改。 我发现问题可以这样解决:y=x[:]

但是它不适用于2D阵列。 例如:

x = [[1,2],[3,4]]
y = x[:]
y[0][0]= 1000
print x

返回[ [1000, 2], [3, 4] ]。如果我将y=x[:]替换为y = x[:][:],这也无济于事。

有人知道什么是正确且简单的方法吗?

trans by 2020-02-18T02:09:36Z

常量值的直接数组初始化

每当您在C#中使用

new T[length]

数组项设置为默认值T。如果Enumerable.ToArray是引用类型,或者如果T是值类型,则使用默认构造函数T的结果为Enumerable.Repeat(value, length).ToArray()

在我的情况下,我想用值-1初始化Enumerable.Repeat(value, length).ToArray()数组:

var myArray = new int[100];
for (int i=0; i<myArray.Length; i++) { myArray[i] = -1; }

因此,在为阵列保留内存之后,CLR会遍历新分配的内存,并将所有条目设置为default(int)=0。之后,我的代码将所有条目设置为-1。

这使得初始化变得多余。 JIT是否检测到该错误并忽略初始化为0,如果没有,是否可以使用自定义值直接初始化内存的一部分?

使用Enumerable.Repeat(value, length).ToArray()引用C#数组初始化-具有非默认值是不可选项,因为Enumerable.ToArray会分配一个新数组,然后将值复制到该数组中。

trans by 2020-02-18T00:36:15Z

Android-将Spinner配置为使用数组

我以以下方式声明我的微调器(它非常静态,所以我在spinner.getSelectedItem()中有2个字符串数组,用于标题和值)

<Spinner
    android:id="@+id/searchCriteria"
    android:entries="@array/searchBy"
    android:entryValues="@array/searchByValues" />

我期望spinner.getSelectedItem()返回一个数组[title, value]但实际上,它仅返回标题字符串。 是否忽略android:entryValues? 如何获得一个值,而不是标题? 是这仅适用于XML或我需要创建适配器并执行以编程方式?

trans by 2020-02-17T19:47:44Z

检测传递的参数是否为数组? Java脚本

可能重复:
如何检测变量是否为数组

我有一个简单的问题:

如何检测传递给我的javascript函数的参数是否为数组? 我不相信我可以测试:

if (typeof paramThatCouldBeArray == 'array') 

那有可能吗?

我该怎么办?

提前致谢。

trans by 2020-02-17T17:15:22Z

asp.net-C#和匿名对象数组

这样的表达是什么意思?

obj.DataSource = new[]
{
    new {Text = "Silverlight", Count = 10, Link="/Tags/Silverlight" },
    new {Text = "IIS 7", Count = 11, Link="http://iis.net" },
    new {Text = "IE 8", Count = 12, Link="/Tags/IE8" },
    new {Text = "C#", Count = 13, Link="/Tags/C#" },
    new {Text = "Azure", Count = 13, Link="?Tag=Azure" }
};

特别是这些行:new {Text =“ IIS 7” ...}

我如何手动创建这样的数组以适合此DataSource。

trans by 2020-02-17T05:04:49Z

.net-VB.NET空字符串数组

如何创建一个空的一维字符串数组?

trans by 2020-02-16T05:32:27Z

在Javascript数组映射中使用Promise函数

具有对象数组[obj1,obj2]

我想使用Map函数对所有它们进行数据库查询(使用promise)并将查询结果附加到每个对象。

[obj1, obj2].map(function(obj){
  db.query('obj1.id').then(function(results){
     obj1.rows = results
     return obj1
  })
})

当然这是行不通的,输出数组是[undefined,undefined]

解决这样的问题的最佳方法是什么? 我不介意使用其他类似异步的库

trans by 2020-02-15T10:44:22Z

如何在PHP中将具有已知键的数组元素移动到数组的末尾?

大脑冻结了一个相当琐碎的问题。 如果我从这样的数组开始:

$my_array = array(
                  'monkey'  => array(...),
                  'giraffe' => array(...),
                  'lion'    => array(...)
);

...并且新元素可能会添加不同的键,但始终是数组值。 现在,我可以确定第一个元素将始终具有键“ monkey”,但不能确定其他任何键。

当我完成数组的填充后,我想将已知元素'monkey'移动到数组的末尾,而又不影响其他元素的顺序。 最有效的方法是什么?

我能想到的每一种方式似乎都有些笨拙,我感觉自己缺少明显的东西。

trans by 2020-02-15T05:51:38Z

java-为什么Collections.sort()比Arrays.sort()慢得多?

我尝试做一个关于Collections.sort()Arrays.sort()的测试。在测试中,我创建了一个ints数组,其长度为1e5,其长度为100次,其中包含从1到1e5的随机数。 我还创建了一个Integer类型的列表,该列表在与数组相同的位置包含相同的值。 然后,我使用Arrays.sort()对数组进行了排序,并使用Collections.sort()对列表进行了排序。


更新:正如@Holger指出的那样,我的代码有一个错误。 现在更正的代码是:

import java.util.* ;


class TestClass {
    public static void main(String args[] ) throws Exception {
        double ratSum = 0 ;
        for(int j=0;j<100;j++)
        {
        int[] A = new int[(int)1e5] ;
        List<Integer> L = new ArrayList<Integer>() ;
        for(int i=0;i<A.length;i++)
        {
            int no = (int)(Math.random()*(int)1e5) ;
            A[i] = no ;
            L.add(A[i]) ;
        }

        long startTime = System.nanoTime() ;
        Arrays.sort(A) ;
        long endTime = System.nanoTime() ;
        Collections.sort(L) ;
        long endTime2 = System.nanoTime() ;
        long t1 = (endTime-startTime), t2 = (endTime2-endTime) ;
        ratSum+=(double)t2/t1 ;
        System.out.println("Arrays.sort took :"+t1+" Collections.sort took :"+t2+" ratio :"+((double)t2/t1)) ;
    }
    System.out.println("Average ratio :"+(ratSum/100)) ;
    }
}

输出为:

Arrays.sort took :24106021 Collections.sort took :92353602 ratio :3.8311425182944956
Arrays.sort took :8672831 Collections.sort took :50936497 ratio :5.873110752417521
Arrays.sort took :8561227 Collections.sort took :25611480 ratio :2.991566512603859
Arrays.sort took :7123928 Collections.sort took :17368785 ratio :2.4380910362934607
Arrays.sort took :6280488 Collections.sort took :16929218 ratio :2.6955258890710403
Arrays.sort took :6248227 Collections.sort took :16844915 ratio :2.695951187432851
Arrays.sort took :6220942 Collections.sort took :16979669 ratio :2.7294369566538315
Arrays.sort took :6213841 Collections.sort took :17439817 ratio :2.8066081832476883
Arrays.sort took :6286385 Collections.sort took :19963612 ratio :3.175690321225951
Arrays.sort took :6209668 Collections.sort took :17008307 ratio :2.7390042430609816
Arrays.sort took :6286623 Collections.sort took :17007163 ratio :2.705293923303497
Arrays.sort took :6256505 Collections.sort took :16911950 ratio :2.703098614961548
Arrays.sort took :6225031 Collections.sort took :16914494 ratio :2.7171742598550916
Arrays.sort took :6233918 Collections.sort took :17005995 ratio :2.72797861633727
Arrays.sort took :6210554 Collections.sort took :17606028 ratio :2.834856278522013
Arrays.sort took :6239384 Collections.sort took :20342378 ratio :3.260318326296314
Arrays.sort took :6207695 Collections.sort took :16519089 ratio :2.6610664666997974
Arrays.sort took :6227147 Collections.sort took :16605884 ratio :2.666692146499834
Arrays.sort took :6225187 Collections.sort took :16687597 ratio :2.680657946500242
Arrays.sort took :6152338 Collections.sort took :16475373 ratio :2.6779043999208105
Arrays.sort took :6184746 Collections.sort took :16511024 ratio :2.6696365541931715
Arrays.sort took :6130221 Collections.sort took :16578032 ratio :2.7043122915144493
Arrays.sort took :6271927 Collections.sort took :16507152 ratio :2.631910734930429
Arrays.sort took :6232482 Collections.sort took :16562166 ratio :2.657394919070765
Arrays.sort took :6218992 Collections.sort took :16552468 ratio :2.661599821964717
Arrays.sort took :6230427 Collections.sort took :21954967 ratio :3.52383022865046
Arrays.sort took :8204666 Collections.sort took :16607560 ratio :2.024160398485447
Arrays.sort took :6272619 Collections.sort took :22061291 ratio :3.5170781136236715
Arrays.sort took :8618253 Collections.sort took :19979549 ratio :2.3182829513127543
Arrays.sort took :6198538 Collections.sort took :17002645 ratio :2.743008915973412
Arrays.sort took :6265018 Collections.sort took :17079646 ratio :2.7261926462142645
Arrays.sort took :6302335 Collections.sort took :17040082 ratio :2.7037728080148073
Arrays.sort took :6293948 Collections.sort took :17133482 ratio :2.722215372608735
Arrays.sort took :6272364 Collections.sort took :17099717 ratio :2.7261997231028046
Arrays.sort took :6219540 Collections.sort took :17026849 ratio :2.737637992520347
Arrays.sort took :6231000 Collections.sort took :17149439 ratio :2.7522771625742255
Arrays.sort took :6309215 Collections.sort took :17118779 ratio :2.713297771592821
Arrays.sort took :6200511 Collections.sort took :17123517 ratio :2.7616299688848227
Arrays.sort took :6263169 Collections.sort took :16995685 ratio :2.7135919532109063
Arrays.sort took :6212243 Collections.sort took :17101848 ratio :2.7529264389689843
Arrays.sort took :6247580 Collections.sort took :17089850 ratio :2.735435160494143
Arrays.sort took :6283626 Collections.sort took :17088109 ratio :2.7194662763188004
Arrays.sort took :6312678 Collections.sort took :17055856 ratio :2.7018415955954036
Arrays.sort took :6222695 Collections.sort took :17071263 ratio :2.7433873908330715
Arrays.sort took :6300990 Collections.sort took :17016171 ratio :2.7005551508572463
Arrays.sort took :6262923 Collections.sort took :17084477 ratio :2.727875945465081
Arrays.sort took :6256482 Collections.sort took :17062232 ratio :2.7271287602202006
Arrays.sort took :6259643 Collections.sort took :17036036 ratio :2.721566709155778
Arrays.sort took :6248649 Collections.sort took :16944960 ratio :2.711779778316881
Arrays.sort took :6264515 Collections.sort took :16986876 ratio :2.7116027338109974
Arrays.sort took :6241864 Collections.sort took :17367903 ratio :2.782486609769133
Arrays.sort took :6297429 Collections.sort took :17080086 ratio :2.7122316107097038
Arrays.sort took :6184084 Collections.sort took :17584862 ratio :2.843567778186713
Arrays.sort took :6315776 Collections.sort took :22279278 ratio :3.5275598754610678
Arrays.sort took :6253047 Collections.sort took :17091694 ratio :2.7333384828228544
Arrays.sort took :6291188 Collections.sort took :17147694 ratio :2.725668665441249
Arrays.sort took :6327348 Collections.sort took :17034007 ratio :2.6921242517402235
Arrays.sort took :6284904 Collections.sort took :17049315 ratio :2.712740719667317
Arrays.sort took :6190436 Collections.sort took :17143853 ratio :2.7694096183209065
Arrays.sort took :6301712 Collections.sort took :17070237 ratio :2.7088253160411013
Arrays.sort took :6208193 Collections.sort took :17060372 ratio :2.74804149935416
Arrays.sort took :6247700 Collections.sort took :16961962 ratio :2.7149130079869392
Arrays.sort took :6344996 Collections.sort took :17084627 ratio :2.6926143058246215
Arrays.sort took :6214232 Collections.sort took :17150324 ratio :2.759846108095095
Arrays.sort took :6224359 Collections.sort took :17081254 ratio :2.744259127727048
Arrays.sort took :6256722 Collections.sort took :17005451 ratio :2.7179489515436357
Arrays.sort took :6286439 Collections.sort took :17061112 ratio :2.713954911516679
Arrays.sort took :6250634 Collections.sort took :17091313 ratio :2.7343327092899696
Arrays.sort took :6252900 Collections.sort took :17041659 ratio :2.7254008540037424
Arrays.sort took :6222192 Collections.sort took :17125062 ratio :2.75225547524088
Arrays.sort took :6227037 Collections.sort took :17013314 ratio :2.7321684454420296
Arrays.sort took :6223609 Collections.sort took :17086112 ratio :2.745370411283871
Arrays.sort took :6280777 Collections.sort took :17091821 ratio :2.7212908530266238
Arrays.sort took :6254551 Collections.sort took :17148242 ratio :2.741722307484582
Arrays.sort took :6250927 Collections.sort took :17053331 ratio :2.7281283240069834
Arrays.sort took :6270616 Collections.sort took :17067948 ratio :2.721893351466586
Arrays.sort took :6223093 Collections.sort took :17034584 ratio :2.737317922132933
Arrays.sort took :6286002 Collections.sort took :17128280 ratio :2.7248289135129133
Arrays.sort took :6239485 Collections.sort took :17032062 ratio :2.7297224049741287
Arrays.sort took :6191290 Collections.sort took :17017219 ratio :2.748574045150526
Arrays.sort took :6134110 Collections.sort took :17069485 ratio :2.782715830006309
Arrays.sort took :6207363 Collections.sort took :17052862 ratio :2.747199092432648
Arrays.sort took :6238702 Collections.sort took :17056945 ratio :2.734053493819708
Arrays.sort took :6185356 Collections.sort took :17006088 ratio :2.749411351585907
Arrays.sort took :6309226 Collections.sort took :17056503 ratio :2.703422416632405
Arrays.sort took :6256706 Collections.sort took :17082903 ratio :2.7303349398229675
Arrays.sort took :6194988 Collections.sort took :17069426 ratio :2.7553606237816766
Arrays.sort took :6184266 Collections.sort took :17054641 ratio :2.757746998592881
Arrays.sort took :6271022 Collections.sort took :17086036 ratio :2.724601508334686
Arrays.sort took :6246482 Collections.sort took :17077804 ratio :2.733987546910405
Arrays.sort took :6194985 Collections.sort took :17119911 ratio :2.763511291794895
Arrays.sort took :6319199 Collections.sort took :17444587 ratio :2.760569337980969
Arrays.sort took :6262827 Collections.sort took :17065589 ratio :2.7249018693954024
Arrays.sort took :6301245 Collections.sort took :17195611 ratio :2.728922776371971
Arrays.sort took :6214333 Collections.sort took :17024645 ratio :2.739577199998777
Arrays.sort took :6213116 Collections.sort took :17382033 ratio :2.7976353572024086
Arrays.sort took :6286394 Collections.sort took :17124874 ratio :2.7241171965995132
Arrays.sort took :6166308 Collections.sort took :16998293 ratio :2.756640278104824
Arrays.sort took :6247395 Collections.sort took :16957056 ratio :2.7142602636779007
Arrays.sort took :6245054 Collections.sort took :16994147 ratio :2.72121698227109
Average ratio :2.792654880602193

此外,我在本地运行了1000次代码(而不是100次),平均比例为::3.0616因此,该比率仍然很大,因此值得讨论。

题:为什么Collections.sort()用大约Arrays.sort()的时间对相同值进行排序的3倍?是因为现在我们不比较基元吗? 为什么要花更多时间?

trans by 2020-02-14T20:16:46Z

Swi中的多维数组

编辑:正如Adam Washington指出的那样,从Beta 6开始,此代码按原样工作,因此该问题不再相关。

我正在尝试创建和遍历二维数组:

var array = Array(count:NumColumns, repeatedValue:Array(count:NumRows, repeatedValue:Double()))

array[0][0] = 1
array[1][0] = 2
array[2][0] = 3
array[0][1] = 4
array[1][1] = 5
array[2][1] = 6
array[0][2] = 7
array[1][2] = 8
array[2][2] = 9

for column in 0...2 {
    for row in 0...2 {
        println("column: \(column) row: \(row) value:\(array[column][row])")
    }
}

但是,这是我得到的输出:

column: 0 row: 0 value:3.0
column: 0 row: 1 value:6.0
column: 0 row: 2 value:9.0
column: 1 row: 0 value:3.0
column: 1 row: 1 value:6.0
column: 1 row: 2 value:9.0
column: 2 row: 0 value:3.0
column: 2 row: 1 value:6.0
column: 2 row: 2 value:9.0

似乎行中的最后一列正在覆盖其他列的值。

我是说错了吗?

编辑:也许从操场上的图片会有所帮助:

Captured from Playground

trans by 2020-02-14T20:07:13Z

python-具有特定比例的二进制随机数组?

什么是有效的(可能用Matlab术语向量化)生成随机数的零和一个特定比例的数的方法? 特别是和Numpy在一起?

由于我的案件是K/N的特例,因此我的代码是:

import numpy as np 
a=np.mod(np.multiply(np.random.randomintegers(0,2,size)),3)

但是,是否至少在K/N(其中K和N是自然数)的情况下,是否有任何内置函数可以更有效地处理此问题?

trans by 2020-02-14T12:52:53Z

PHP-如何获取数组中最后一个元素之前的元素?

如何从PHP 5中的数组中获取最后一个元素之前的元素?

trans by 2020-02-14T07:09:02Z

如何在JavaScript数组中将整数分隔为单独的数字?

到目前为止,这是我的代码:

var n = 123456789;
var d = n.toString().length;
var digits = [];
var squaredDigits = [];
for (i = d; i >= 1; i--) {
    var j = k / 10;
    var r = (n % k / j) - 0.5;
    var k = Math.pow(10, i);
    var result = r.toFixed(); 
    digits.push(result);
}

console.log(digits);

但是,当我运行我的代码时,我得到的是:[9, 1, 2, 3, 4, 5, 6, 7, 8]

如果有人能看到问题或找到更好的解决方案,我将不胜感激!

trans by 2020-02-13T17:14:00Z

javascript-如何使用条件元素定义数组?

我如何定义条件数组元素?我想做这样的事情:

const cond = true;
const myArr = ["foo", cond && "bar"];

如预期的那样工作:cond

但是,如果我将cond设置为false,则会得到以下结果:["foo", false]

如何定义带有条件元素的数组?

trans by 2020-02-13T10:10:34Z

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 下一页 共54页