我是Java的新手,我正在学习封装,并看到了一个示例,其中实例变量在类中声明为私有.
http://www.tutorialspoint.com/java/java_encapsulation.htm
我有2个查询:
你可以用一个例子来解释,如果在Java中的类中将实例变量声明为public,会出现什么问题?
我试图将空格字符替换为我的字符串中的连字符.
String replaceText = "AT AT";
replaceText.replace(' ', '-');
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时,我似乎无法取代这个角色.我尝试了这种replaceAll()方法,它也无法正常工作.
++++++回答+++++++
对不起我的错误..深夜编程的结果:(
谢谢你的答案,我可能无法回答所有,所以我会检查第一个答案
replaceText = replaceText.replace(' ', '-');
Run Code Online (Sandbox Code Playgroud) 需要一个Java函数来查找两个字符串的交集.即字符串共有的字符.
例:
String s1 = new String("Sychelless");
String s2 = new String("Sydney");
Run Code Online (Sandbox Code Playgroud) 对于这个问题,字符串中的"对"被定义为一个字符的两个实例被另一个字符分隔的情况.因此,在"AxA"中,A成为一对.对可以重叠,因此"AxAxA"包含三对; 两个用于A,一个用于x.
更多例子:
countPairs("axa")→1
countPairs("axax")→2
countPairs("axbx")→1
我被问到昨天在面试中如何计算给定字符串中的对数,我不知道该怎么做.
我已经转换为Core Java中的Map问题.
以下是要求:
给定下面的String数组
String str[] = {"abc","123","def","456","ghi","789","lmn","101112","opq"};
Run Code Online (Sandbox Code Playgroud)
将其转换为Map,使得结果输出低于
产量
====== ====== key Value ====== ====== abc true 123 false def true 456 false
应该为数组中的每个元素打印上面的内容.我已经编写了代码,但它没有工作,我被卡住了.请让我知道如何解决.提前致谢.
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class CoversionToMap {
/**
* @param args
*/
public static void main(String[] args) {
String str[] = {"abc","123","def","456","ghi","789","lmn","101112","opq"};
Map m = new HashMap();
for(int i=0;i<str.length;i++){
if(Integer.parseInt(str[i]) < 0){
m.put(str[i],true);
}else{
m.put(str[i],false);
}
}
//Print the map values finally
printMap(m);
}
public static void printMap(Map mp) {
Iterator it …Run Code Online (Sandbox Code Playgroud) enum CoffeeSize{
BIG(8),
HUGE(10),
OVERWHELMING(16) {
public String getLidCode(){
return "A";
}
};
private int ounces;
public int getOunces(){
return ounces;
}
CoffeeSize(int ounces){
this.ounces = ounces;
}
public String getLidCode(){
return "B";
}
}
Run Code Online (Sandbox Code Playgroud)
这是K&B 6书中的SCJP 1.6问题.这是常量特定类主体的一个示例,作为SCJP 6的一个特性.如何执行此操作并查看结果输出?
我有两个问题:
我的Java main方法是什么样的?请帮我执行这个部分代码.我无法理解输出的行为方式.
该getLidCode()方法在Java 1.6中如何在此类主体中工作?
以下是问题陈述:
PS:给定一个字符串和一个非空的子字符串子,递归计算最大的子字符串,它以sub开头和结尾并返回其长度.
Examples:
strDist("catcowcat", "cat") ? 9
strDist("catcowcat", "cow") ? 3
strDist("cccatcowcatxx", "cat") ? 9
下面是我的代码:(没有递归)//因为我发现很难用递归实现.
public int strDist(String str, String sub){
int idx = 0;
int max;
if (str.isEmpty()) max = 0;
else max=1;
while ((idx = str.indexOf(sub, idx)) != -1){
int previous=str.indexOf(sub, idx);
max = Math.max(max,previous);
idx++;
}
return max;
}
Run Code Online (Sandbox Code Playgroud)
它的工作量很少,如下所示,但其他人则返回FAIL.
Expected This Run
strDist("catcowcat", "cat") ? 9 6 FAIL
strDist("catcowcat", "cow") ? 3 3 OK
strDist("cccatcowcatxx", "cat") ? 9 8 FAIL
strDist("abccatcowcatcatxyz", "cat") ? 12 12 …
嗨,SO专家,
第 1 步。我创建了一个 DVDInfo 类,它具有标题、流派、leadActor 以及各自的 getter 和 setter,如下面的代码所示。
问题:我必须从具有“正斜杠 /”的 dvdInfo.txt 文件中填充一个 Arraylist,并且所需的输出应包含不带“正斜杠 /”的 arrayList。
------------------------------------
dvdInfo.txt file
------------------------------------
Donnie Darko/sci-fi/Gyllenhall, Jake
Raiders of the Lost Ark/action/Ford, Harrison
2001/sc-fi/??
Caddy Shack/comedy/Murray, Bill
Star Wars/sc-fi/Ford, Harrison
Lost in Translation/comedy/Murray, Bill
Patriot Games/action/Ford, Harrison
-------------------------------------
import java.util.*;
import java.io.*;
class DVDInfo{
private String title;
private String genre;
private String leadActor;
DVDInfo(String t,String g,String a){
this.title = t;
this.genre =g;
this.leadActor=a;
}
public String getTitle() {
return title;
}
public void …Run Code Online (Sandbox Code Playgroud)