这是我希望 Java 的 String 类有一个 replaceLast 方法的地方,但它没有,而且我的代码得到了错误的结果。
我正在编写一个程序,用于在数据结构中搜索与字符串前缀匹配的任何项目。但是,由于我使用的是迭代器,因此 iter.next() 调用返回的最后一项与模式不匹配,因此我想更改搜索字符串,以便查询的最后一个字符增加一个字母. 我的测试代码返回 [C@b82368 带有此代码和An标题搜索:
public String changeLastCharacter(String titleSearch) {
char[] temp= titleSearch.toCharArray();
char lastLetter= temp[temp.length-1];
lastLetter++;
temp[temp.length-1]= lastLetter;
String newTitleSearch= temp.toString();
return newTitleSearch;
}
Run Code Online (Sandbox Code Playgroud)
首先,这段代码输出的原因是什么?其次,有没有更好的方法来执行我的解决方案?
在数据结构中,我知道结构的大小取决于从一个部分到另一个部分的内部链接.有没有办法,除了JProfiler之外,确切地说明在特定结构中有多少内存?
例如,本学期的课程项目与将各种结构应用于歌曲数据库有关.这些项目涵盖了数组,列表,展开的列表和树.我想做的是看看使用了多少内存.例如,链表的内存要求为3N,但我想看看节点在我的项目中占用了多少空间.
JProfiler看起来会起作用,但500美元超出了我的价格范围,而且我想将它用于本学期涵盖的所有结构,而不是目前为止应用的三种结构.
我正在从我的数据结构类项目中调试错误的搜索返回.当前项目要求我们构建有序的展开链表,并对内容进行搜索,然后将项目子列表从包含起点返回到独占终点.为了做到这一点,我必须搜索内部数组以找到start元素的索引点.我通过二分搜索来做到这一点,但由于这只返回第一个找到的匹配,并且在它之前可能还有其他匹配,我必须在数组中返回才能找到第一个真正的匹配.我通过这样做
//get first index match, work backwards
int index= binarySearch(node.items, 0, node.numUsed, item, comp);
while (index>0 && comp.compare(node.items[index], item)==0){
index--;
}
Run Code Online (Sandbox Code Playgroud)
教授提供了测试代码,用于分解字符串并将每个字符添加为结构中的项目.他还包括一个嵌套的StringCmp类,它comp在上面的二进制搜索声明中被引用.他的比较方法是
public int compare(String s1, String s2) {
cmpCnt++;
return s1.compareTo(s2);
}
Run Code Online (Sandbox Code Playgroud)
但是,当在子列表方法上从i运行测试到o时,此方法返回一个真值comp.compare(h,i)==0,这是从我写的搜索类中抛出我的开始结果.我最初return index++通过结构测试得到了补偿,但是把预期的起点丢了一个.
那么为什么当它显然是假的时候才会真实地回归呢?
编辑添加了子列表方法的打印输出,预计从i运行到o
输入测试字符串= abcdefghijklmnopqrstuvwxyzaeiou
从子列表方法返回:
块1(使用4的10):[h] [i] [i] [j]
块2(使用4的10):[k] [l] [m] [n ]
h根本不应该在列表中,但是comp.compare(node.items[index], item)==0返回true,即i == h,这显然是假的.
编辑二 项目的第二部分要求我们解析文本文件,从Artist,Title和Lyrics字段构建Song对象,然后使用前缀对标题进行搜索.这里发生的错误不会发生在单字母和多字母搜索中,所以我认为问题在于测试中的StringCmp嵌套类.
我当前的项目让我们在Java中使用TreeSet和TreeMap,从文本文件中读入10514个Song元素的输入数组.每首歌曲包含艺术家,标题和抒情字段.该项目的目的是使用集合和地图对歌词进行快速搜索.
首先,我遍历输入的Song数组,访问lyrics字段并创建一个Scanner对象,使用此代码迭代歌词: commonWords是一个不应该是键的单词TreeSet,lyricWords是歌曲的整个单词映射.
public void buildSongMap() {
for (Song song:songs) {
//method variables
String currentLyrics= song.getLyrics().toLowerCase();
TreeSet<Song> addToSet=null;
Scanner readIn= new Scanner(currentLyrics);
String word= readIn.next();
while (readIn.hasNext()) {
if (!commonWords.contains(word) && !word.equals("") && word.length()>1) {
if (lyricWords.containsKey(word)) {
addToSet= lyricWords.get(word);
addToSet.add(song);
word=readIn.next();
} else
buildSongSet(word);
} else
word= readIn.next();
}
}
Run Code Online (Sandbox Code Playgroud)
为了构建songSet,我使用以下代码:
public void buildSongSet(String word) {
TreeSet<Song> songSet= new TreeSet<Song>();
for (Song song:songs) {
//adds song to set
if (song.getLyrics().contains(word)) {
songSet.add(song);
}
}
lyricWords.put(word, songSet); …Run Code Online (Sandbox Code Playgroud) 我正在使用开源perl脚本来创建基于英语维基百科转储的文本语料库.已经提取了纯文本,但是仍然需要删除各种标点符号等.但是,此脚本的输出实际上创建了一个包含单行的7.2GiB文本文件.由于我的需要,我想改变脚本,使其每20个字插入一个新的行字符.
到目前为止,我试过这个:
$wordCount=0;
while (<STDIN>) {
$wordCount++;
//text processing regex commands here
# Remove ellipses
s/\.\.\./ /g;
# Remove dashes surrounded by spaces (e.g. phrase - phrase)
s/\s-+\s/ /g;
# Remove dashes between words with no spaces (e.g. word--word)
s/([A-Za-z0-9])\-\-([A-Za-z0-9])/$1 $2/g;
# Remove dash at a word end (e.g. three- to five-year)
s/(\w)-\s/$1 /g;
# Remove some punctuation
s/([\"\?,;:%???!()\[\]{}<>_\.])/ /g;
# Remove trailing space
s/ $//;
# Remove double single-quotes
s/'' / /g;
s/ ''/ /g;
# Replace accented e …Run Code Online (Sandbox Code Playgroud) 目前,我正在调整注册页面以使用Ajax/jQuery.它在简单的阶段,只需要用户名和密码.
我想使用jQuery告诉用户用户名文本框中的当前条目是无效还是已被采用.如果满足条件,则div namemsg将添加文本并执行fadeIn事件.
页面加载时禁用提交按钮,并在通过验证时启用按钮.为此,我有以下ajax请求来处理用户名验证
$("#username").keyup(function(){
//get current value
thisVal = $(this).val();
//execute request to see if current val exists in database
$.ajax({
type: "GET",
url: "includes/phpscripts.php?action=checkUser",
data: {username:thisVal},
success: function(data){
//if username is already taken, show warning and disable submit button
if (data == 'true'){
$("#namemsg").text("Username is already taken");
$("#namemsg").fadeIn(100);
$("#submit").attr('disabled', 'disabled');
}
//otherwise, possible valid username
else if (data == 'false'){
//check if username is of the proper length. If not, display
//error and disable submit button
if ($(this).val().length …Run Code Online (Sandbox Code Playgroud) 给定一个PHP类具有几种函数的样式
public function $op_Data() {
$command = Yii::app()->db->createCommand("");
$transaction = Yii::app->db->beginTransaction();
try {
//code here
$transaction->commit()
} catch (Exception $e) {
$transaction->rollback();
return "Operation Failed\n" . $e->getTraceAsString();
}
}
Run Code Online (Sandbox Code Playgroud)
在提交操作成功或返回错误后,是否可以继续使用先前方法中的事务?
我的意思是,调用插入函数.它检查事务是否处于活动状态并使用它.如果事务处于非活动状态,则会创建一个事务.对需要事务功能的所有函数进行相同的检查.
如果存在此功能,它是否会影响特定功能中的提交/回滚操作?
如果你有一个不断渲染/销毁的HTML元素,那么对HTML的javascript事件绑定是否仍然存在,或者是否需要在创建/销毁周期中绑定/取消绑定事件?
我正在使用D3来生成美国各县的地图.另外,我正在生成工具提示覆盖,其中包含点击事件上的按钮以进行有效选择.
click事件处理程序的一部分,我将模板的HTML绑定到tooltip元素,然后将javascript事件处理程序绑定到所述HTML
thisObj._tooltip.template = template : "<div id = 'tooltip_template'>" +
"<div class = 'county_data'></div>" +
"<img src = '/static/images/delete.png' width = '28' height = '28' class = 'delete_logo' id = 'close_tooltip' />" +
"<button id = 'add_prospective_market' class = 'tooltip_button'>Prospective Market</button>" +
"<button id = 'add_market' class = 'tooltip_button'>Market County</button>" +
"<button id = 'remove_market' class = 'tooltip_button'>Remove Market</button></div>"
thisObj._tooltip.tooltip.html(thisObj._tooltip.template)
.style("left", (d3.event.pageX + 10) + "px")
.style("top", (d3.event.pageY - 50) + "px")
.style("pointer-events" , "auto")
.style("width", "400px")
.style("height", …Run Code Online (Sandbox Code Playgroud) 我的教授在课堂上引用了这个例子.它基本上是Unix more命令的一个版本,我不确定其中的几个东西
int main( int ac , char *av[] )
{
FILE *fp;
if ( ac == 1 )
do_more( stdin );
else
while ( --ac )
if ( (fp = fopen( *++av , "r" )) != NULL )
{
do_more( fp ) ;
fclose( fp );
}
else
exit(1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我理解*fp定义了一个文件指针,而*av []是命令行参数的数组.但*++av在操作方面意味着什么呢?
我正在为Web开发类构建一个简单的Grails应用程序.在大多数情况下,应用程序已完成,但我有一个棘手的问题.
在索引页,我有一系列的按钮相对应的List,Create以及那些通过脚手架建于Grails的其他模板.如何动态传递控制器操作的正确路径?
为此,我需要获取当前页面URL并添加正确的位置.这可能在Grails中做,还是应该坚持使用jquery或其他一些ajax解决方案?
对于Web应用程序视图的一部分,用户输入一些数据并将其添加到无序列表中.对于将来的修改,我希望每个列表元素都可以选择进行编辑和/或查看.
HTML:
<div id = "answer_list_container">
<ul id = "answers">
<li id = "answer_1" class = "answer">Answer1 Text</li>
<li id = "answer_2" class = "answer">Answer2 Text</li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
但是,我无法获得list元素的右选择器.我试过了
answerSelectionHandler包含一个console.log在执行处理程序时触发的调用.它不会消失,这表明选择器问题.
java ×4
jquery ×3
javascript ×2
build-time ×1
c ×1
grails ×1
optimization ×1
perl ×1
php ×1
string ×1
transactions ×1
unix ×1
validation ×1
yii ×1