所以基本上我生成随机的10000个IP地址,我想存储在HashSet中找到的所有那些IP地址,但根据我的计算,发现大约6000个ip地址,但在HashSet中只有700个ip地址被存储了?在存储String方面HashSet是否有任何限制.任何建议将不胜感激.
Set<String> ipFine = new HashSet<String>();
long runs = 10000;
while(runs > 0) {
String ipAddress = generateIPAddress();
resp = SomeClass.getLocationByIp(ipAddress);
if(resp.getLocation() != null) {
ipFine.add(ipAddress);
}
runs--;
}
Run Code Online (Sandbox Code Playgroud) 我一直在寻找如何将字符串的第一个字符大写,但我发现的任何东西都没有帮助.为了我的方法,我需要将用户输入的字符串设置为小写.
sourceText = enterText.getText();
char chr = sourceText.charAt(0);
Run Code Online (Sandbox Code Playgroud)
所以我有一个布尔值,如果第一个字符是大写的,则为true.
boolean upperCase = Character.isUpperCase(chr);
sourceTextLower = sourceText.toLowerCase();
Run Code Online (Sandbox Code Playgroud)
很酷的东西发生在这里,最终产品是另一个名为translatedTextString的字符串和一个if语句
String s2 = "";
if(upperCase == true)
{
int x = translatedTextString.length();
s2 = translatedTextString.substring(0,1).toUpperCase().concat(translatedTextString.substring(1, x));
}
//translatedText is a label
translatedText.setText(s2);
Run Code Online (Sandbox Code Playgroud)
但是,当我运行程序时,我的结果的第一个字符仍然是小写.所以我的问题是:这甚至是正确的方法吗?如果是这样,我做错了什么,如果没有,我怎么能正确地做到这一点?
我在Java中实现了一个程序,对于特定的测试用例来说,令人惊讶的是需要177M内存(我没有,因为程序是由一个网站测试的).
问题是找出字符串S2中存在于字符串S1中的所有字符.还有N个这样的案例.
public static void main(String[] args) throws Exception {
BufferedReader bin = new BufferedReader (new InputStreamReader (System.in));
String jewel, stone;
int t = Integer.parseInt (bin.readLine());
int count;
int i;
while (t-->0) {
count = 0;
jewel = bin.readLine();
stone = bin.readLine();
for (i=0; i<stone.length(); i++) {
if (jewel.indexOf(stone.charAt(i)) !=-1) {
count++;
}
}
System.out.println(count);
}
}
Run Code Online (Sandbox Code Playgroud)
我也不明白,它是如何使用177M的ram.即使他们正在测试一个巨大的文件,那么也只有2个字符串.但是代码完全正常,测试用例通过了.
由于java程序占用了大量内存,所以我打算编写一个相同程序的C版本.以下是:
int main ()
{
char * pch;
char jewel[100], stone[100];
int n;
scanf("%d",&n);
int len;
int tp;
int count = 0; …Run Code Online (Sandbox Code Playgroud) 错误消息是:
Using the URLconf defined in Blog.urls,
Django tried these URL patterns, in this order:
^admin/doc/
^admin/
^post/ ^post/(?P<post_id>\d+)/$
The current URL, post/1458/, didn't match any of these.
Run Code Online (Sandbox Code Playgroud)
为什么?我认为post/1485/匹配^post/(?P<post_id>\d+)/$
我的根URL配置是:
urlpatterns = patterns('',
...
url(r'^post/', include('posts.urls')),
)
Run Code Online (Sandbox Code Playgroud)
然后,我posts/urls.py是:
urlpatterns = patterns('',
...
url(r'^post/(?P<post_id>\d+)/$', 'post.views.post_page'),
)
Run Code Online (Sandbox Code Playgroud) 我知道这对某人来说非常简单,我无法弄清楚为什么编译器会抱怨这个.我一直在寻找一些答案,我能找到的只是一个支架问题,但我不认为这是我的问题.我是Java的新手,所以任何帮助都会很棒.这是应该是基本累加器程序的代码.
public class BasicAccumulator implements Accumulator {
{
private int digit;
private int value;
}
public int basicAccumulator(int digit, int value)
{
digit = 0;
value = 0;
}
public void addDigit(int digit);
{
digit = digit + value;
}
public void plus();
{
value = digit + digit;
}
public void minus();
{
value = digit - digit;
}
public void clear();
{
value = 0;
}
public int displayValue();
{
return value;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在通过oracle API java.io.RandomAccessFile上课.
在类中有一个名为read()的方法,它从传递给构造函数的文件中读取数据的字节:
public int read()抛出IOException
从该文件中读取一个字节的数据.该字节以0到255(0x00-0x0ff)范围内的整数形式返回.如果尚未提供输入,此方法将阻止.
尽管RandomAccessFile不是InputStream的子类,但此方法的行为方式与InputStream的InputStream.read()方法完全相同.
返回:数据的下一个字节,如果已到达文件末尾,则返回-1.抛出:IOException - 如果发生I/O错误.如果已达到文件结尾,则不会抛出.
我很困惑,这是否意味着它从传递给构造函数的文件中读取8位数据并将读取的内容转换为int.
有什么建议?
我想知道是否有人确切知道代码行发生时会发生什么:
class Class{}
class Math extends Class{}
class UseClasses{
public static void main (String[]args)
{
new Math(); //Line 8
(Class)new Math();// Line 9
}
Run Code Online (Sandbox Code Playgroud)
我完全理解"new keyword"在堆内存中充当对象实例创建者.但是,在前面的代码中,您可以看到第9行对此关键字(新)的使用不正常.第8行没关系并且编译得很好.但是第9行需要将内容分配给其他一些引用.因此,这意味着每次存在强制转换操作数时,在这种情况下(Class)new Math,将实例化新的引用(强调引用而不是对象).
它以这种方式工作吗?如果没有,你能解释一下为什么它在第8行编译得很好并且它在第9行中出错了吗?(显然由于铸造功能不在那里,但为什么不呢?)
我正在寻找一种简单易懂的算法,按字母顺序对C中的字符数组进行排序.
我从文件中读取时遇到了一些问题.我有两个头文件:a和b.b源自a,c源自b.现在我想打开一个文本文件.
整个格式是这样的:
一个约翰
约翰尼
P 123
如果第一个字符为'a'且'p'也在那里,则打印第二行,否则打印第一行.
#include "c.h"
#include <iostream>
# include <fstream>
using namespace std;
c :: c()
{
ifstream input;
input.open ("abc.txt");
ch = input.get();
input >> ch;
if (ch ='A')
a* z =new a();
else
input.close();
}
Run Code Online (Sandbox Code Playgroud)
谁能给我一些关于如何实现这一目标的建议?
所以我是一个java newby,我正在做这个家庭作业,要求我写一个程序来找到前四个完美的数字.我想我的一切都大致正确,但我无法测试我的程序,因为我尝试运行程序时出现错误消息"线程异常"主"javva.lang.NoSuchMethodError:main".编译得很好,我认为所有数学都是正确的:
import java.awt.*;
public class PerfectNumbers
{
int num = 1, divisor1 = 1, divisor2, divSum, perfNum1, perfNum2, perfNum3, perfNum4;
public void main()
{
do
{
while (num != divSum)
{
if (num % divisor1 == 0 && divisor1 != Math.sqrt(num))
{
divSum += divisor1;
divisor2 = num / divisor1;
divSum += divisor2;
divisor1++;
}
else if (num % divisor1 !=0 && divisor1 != Math.sqrt(num))
divisor1++;
else if (divisor1 == Math.sqrt(num))
{
divSum += Math.sqrt(num);
divisor1 = 1;
} …Run Code Online (Sandbox Code Playgroud)