我目前正在学习Groovy而且我遇到了泛型方法.
我想用泛型返回类型定义泛型方法,该方法是从参数类型推断出来的.
在Java中,签名将是:
<T> T getBean(String name, Class<T> requiredType);
Run Code Online (Sandbox Code Playgroud)
我怎样才能在Groovy中实现它?
# -*- coding: utf-8 -*-
import string
s = u"Dobre uczynki s? jak dobre poematy. Mo?na ?atwo uchwyci?, ku czemu zmierzaj?, lecz nie zawsze da si? je racjonalnie wyt?umaczy?."
exclude = set(string.punctuation)
s = ''.join(ch for ch in s if ch not in exclude)
s = s.split()
print s
Run Code Online (Sandbox Code Playgroud)
打印...
[u'Dobre', u'uczynki', u's\u0105', u'jak', u'dobre', u'poematy', u'Mo\u017cna', u'\u0142atwo', u'uchwyci\u0107', u'ku', u'czemu', u'zmierzaj\u0105', u'lecz', u'nie', u'zawsze', u'da', u'si\u0119', u'je', u'racjonalnie', u'wyt\u0142umaczy\u0107']
Run Code Online (Sandbox Code Playgroud)
它似乎不仅不优雅,而且速度慢.
你能找到更好的解决方案吗?也许使用正则表达式?
我的代码到目前为止:
FileReader fileReader = new FileReader("filename.xml");
Client c = Client.create();
WebResource webResource = c.resource("http://localhost:8080/api/resource");
webResource.type("application/xml");
Run Code Online (Sandbox Code Playgroud)
我要发送的内容filename.xml与POST方法,但我不知道如何将它们添加到请求的身体.我需要帮助,因为在网上我只能找到如何添加Formargs.
提前致谢.
我正在寻找一种左移元组的有效方法.
到目前为止我做了什么:
def leftShift(tup, n):
length = len(tup)
if length != 0:
n = n % length
else:
return tuple()
return tup[n:] + tup[0:n]
sample = (1,2,3,4)
sample2 = ()
print(leftShift(sample, 5)) #prints (2, 3, 4, 1)
print(leftShift(sample, 1)) #prints (2, 3, 4, 1)
print(leftShift(sample, 15)) #prints (4, 1, 2, 3)
print(leftShift(sample, 3)) #prints (4, 1, 2, 3)
print(leftShift(sample2, 4)) #prints ()
Run Code Online (Sandbox Code Playgroud)
移位的位数作为第二个参数给出.
它有效吗?它可以用更多Pythonic方式编码吗?
告诉我,是吗......
length = len(tup)
if length != 0:
n = n % length
Run Code Online (Sandbox Code Playgroud)
效率更高
if len(tup) …Run Code Online (Sandbox Code Playgroud) 输出/查找前n个天然二进制代码:
import math
def binary_print(n):
m = int(math.ceil(math.log(n, 2)))
for i in range(n):
b = str(bin(i)[2:])
print((m - len(b)) * '0' + b)
Run Code Online (Sandbox Code Playgroud)
我的问题是:
你知道在Python中用其他任何方法吗?也许更快?或更短(更少的代码)?
与Apache共享它是非常简单转换InputStream到byte[],
static byte[] toByteArray(InputStream input)
但我找不到相反的方法.将字节数组转换为InputStream对象的最流行/最直接的方法是什么?
提前致谢.
我试图以两种方式反转数组:
1)通过创建一个非常简单的新数组:
public static int[] reverse(int[] array) {
int[] reverseArray = new int[array.length];
for(int i = 0; i < reverseArray.length; i++) {
reverseArray[i] = array[array.length - i - 1];
}
return reverseArray;
}
Run Code Online (Sandbox Code Playgroud)
2)第二种方法我得到了答案,但实际上我并不理解它,它实际上利用交换,将数组的值赋予临时变量然后更改它并将其返回到原始变量:
public static int[] reverse2(int[] array)
{
for (int i=0; i < array.length / 2; i++)
{
int temp = array[i];
array[i] = array[array.length - i - 1];
array[array.length - i - 1] = temp;
}
return array;
}
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释第二个代码吗?我不明白除2?如果数组大小是偶数还是奇数,会发生什么?
我正在用正则表达式开始冒险.我有兴趣拆分特殊格式的字符串.如果一个字母不在括号内,它应该成为输出列表的不同元素.括号内的字母应放在一起.
样品:
我的字符串=>通缉名单
"ab(hpl)x" => ['a', 'b', 'hpl', 'x']"(pck)(kx)(sd)" => ['pck', 'kx', 'sd']"(kx)kxx(kd)" => ['kx', 'k', 'x', 'x', 'kd']"fghk" => ['f', 'g', 'h', 'k']如何用正则表达式实现re.split?在此先感谢您的帮助.
我有一个分配问题,我需要在不使用'sort'命令的情况下解决.我很感激任何建议.
问题:假设你有两个int数组,arr1和arr2,每个都包含按升序排序的整数.
编写一个名为merge的静态方法,它接收这两个数组的参数,并返回对新的,有序的int数组的引用,这是合并arr和arr2的整数的结果.
今天我一直在使用Boost :: shared_ptr,我有一个问题.
vector<shared_ptr<KlasaA> > vec;
vec.push_back(shared_ptr<KlasaA>(new KlasaB));
vec.push_back(shared_ptr<KlasaA>(new KlasaC));
vec.push_back(shared_ptr<KlasaA>(new KlasaC));
vec.push_back(shared_ptr<KlasaA>(new KlasaA));
for (vector<shared_ptr<KlasaA> >::const_iterator c_it = vec.begin();
c_it != vec.end(); ++c_it)
{
cout << c_it->get()->foo(10) << endl;
}
Run Code Online (Sandbox Code Playgroud)
上面的循环遍历向量并以多态方式调用foo(10).
我的问题是:
能够...
for (vector<shared_ptr<KlasaA> >::const_iterator c_it = vec.begin();
c_it != vec.end(); ++c_it)
Run Code Online (Sandbox Code Playgroud)
和
cout << c_it->get()->foo(10) << endl;
Run Code Online (Sandbox Code Playgroud)
以更简洁的方式表达?提前致谢.
我有以下内容:
def userInstance=User.findAll("from User u where u.merchant is not null and u.merchant.id = ? and u.authorities.authority.contains('ROLE_MERCHANT')", merchantId)
Run Code Online (Sandbox Code Playgroud)
并得到以下错误:
org.hibernate.hql.ast.QuerySyntaxException: unexpected AST node: ( near line 1, column 121 [from com.testing.tests.User u where u.merchant is not null and u.merchant.id = ? and u.authorities.authority.contains('ROLE_MERCHANT')]
at $Proxy20.createQuery(Unknown Source)
at testing.admin.UserService.findByMerchantId(UserService.groovy:14)
Run Code Online (Sandbox Code Playgroud)
如何仅返回具有特定角色的用户?
谢谢