JAVA编程
随机生成10个[a-z]的字符存放在字符串数组str中,然后使用Map的key来保存str中的元素,value保存key在str中出现的次数。
import java.util.TreeMap;
public class Test {
// 统计数字或者字符出现的次数
public static TreeMap<Character, Integer> Pross(String str) {
char[] charArray = str.toCharArray();
TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();
for (int x = 0; x < charArray.length; x++) {
if (!tm.containsKey(charArray[x])) {
tm.put(charArray[x], 1);
} else {
int count = tm.get(charArray[x]) + 1;
tm.put(charArray[x], count);
}
}
return tm;
}
public static void main(String[] args) {
String temp = "abcdefghijklmnopqrstuvwxyz";
String str = "";
int randNum;
for(int i = 0; i < 10; i++){
randNum = (int)((26)*Math.random());
str += temp.charAt(randNum);
}
System.out.println("生成的新字符串:" + str);
TreeMap<Character, Integer> tm = Pross(str);
System.out.println(tm);
}
}追问
while(i.hasNext()){
System.out.println(i.next());
}追问
import java.util.Iterator;
public class Test {
public static void main(String[] args) {
ArrayList<String> a = new ArrayList<String>();
a.add("GHJ");
a.add("ADFD");
a.add("Tsdf");
Iterator i = a.iterator();
while(i.hasNext()){
System.out.println(i.next());
}
}
}
import java.util.Iterator;
public class Test {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "china");
map.put(2, "usa");
map.put(3, "english");
Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
Object key = iter.next();
Object val = map.get(key);
System.out.println("键--" + key + ", 值--" + val);
}
}
}追问
import java.util.Iterator;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "1232");
map.put(2, "1232");
map.put(4, "1232");
Iterator iter = map.keySet().iterator();
while(iter.hasNext()){
Object key = iter.next();
Object val = map.get(key);
System.out.println(key + " " + val);
}
}
}
public class Test {
// 统计数字或者字符出现的次数
public static TreeMap<Character, Integer> Pross(String str) {
char[] charArray = str.toCharArray();
TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();
for (int x = 0; x < charArray.length; x++) {
if (!tm.containsKey(charArray[x])) {
tm.put(charArray[x], 1);
} else {
int count = tm.get(charArray[x]) + 1;
tm.put(charArray[x], count);
}
}
return tm;
}
public static void main(String[] args) {
String temp = "abcdefghijklmnopqrstuvwxyz";
String str = "";
int randNum;
for(int i = 0; i < 10; i++){
randNum = (int)((26)*Math.random());
str += temp.charAt(randNum);
}
System.out.println("生成的新字符串:" + str);
TreeMap<Character, Integer> tm = Pross(str);
System.out.println(tm);
}
}追问
谢谢😁
追答请采纳。
追问我还有很多java问题 都不会 你java好吗 能教我吗😊
追答可以的。
追问我这是错哪了
怎么改呀
追答后面的字母I变成小写。
Iterator i = a.iterator();while(i.hasNext()){
System.out.println(i.next());
}追问
下面一行 输出那句还是错的呀
追答你把上面的看完先。
追问ArrayList存储任意三个字符串,并遍历(迭代器遍历)ArrayList中的所有元素。(注意:使用while循环)
这是问题
追答import java.util.ArrayList;import java.util.Iterator;
public class Test {
public static void main(String[] args) {
ArrayList<String> a = new ArrayList<String>();
a.add("GHJ");
a.add("ADFD");
a.add("Tsdf");
Iterator i = a.iterator();
while(i.hasNext()){
System.out.println(i.next());
}
}
}
看到不到我贴出来的代码?
看到了
谢谢
问题:
HashMap的键是Integer,值是String,存储三对元素,并遍历。(根据键找值的方式遍历)
import java.util.Iterator;
public class Test {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "china");
map.put(2, "usa");
map.put(3, "english");
Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
Object key = iter.next();
Object val = map.get(key);
System.out.println("键--" + key + ", 值--" + val);
}
}
}追问
您好,请问这个要怎么改呢
追答import java.util.HashMap;import java.util.Iterator;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "1232");
map.put(2, "1232");
map.put(4, "1232");
Iterator iter = map.keySet().iterator();
while(iter.hasNext()){
Object key = iter.next();
Object val = map.get(key);
System.out.println(key + " " + val);
}
}
}
下次代码不要贴图片上来, 贴代码文本上来, 方便一点
温馨提示:答案为网友推荐,仅供参考
第1个回答 2016-05-06
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class SimpleRandom {
public static void main(String[] args) {
Map<String, Integer> randomResultMap = new HashMap<String, Integer>();
String[] randomResultArr = new String[10];
// A = 65, a = 97
int idx = 0;
while(idx < randomResultArr.length) {
String randomChar = String.valueOf((char) (Math.random() * 26 + 97));
randomResultArr[idx] = randomChar;
if (randomResultMap.containsKey(randomChar)) {
randomResultMap.put(randomChar, randomResultMap.get(randomChar) + 1);
} else {
randomResultMap.put(randomChar, 1);
}
idx++;
}
// Test
System.out.println(Arrays.toString(randomResultArr));
for(Map.Entry<String, Integer> entry : randomResultMap.entrySet()) {
System.out.println(String.format("Key=%s, Count=%d", entry.getKey(), entry.getValue()));
}
}
}追问
import java.util.HashMap;
import java.util.Map;
public class SimpleRandom {
public static void main(String[] args) {
Map<String, Integer> randomResultMap = new HashMap<String, Integer>();
String[] randomResultArr = new String[10];
// A = 65, a = 97
int idx = 0;
while(idx < randomResultArr.length) {
String randomChar = String.valueOf((char) (Math.random() * 26 + 97));
randomResultArr[idx] = randomChar;
if (randomResultMap.containsKey(randomChar)) {
randomResultMap.put(randomChar, randomResultMap.get(randomChar) + 1);
} else {
randomResultMap.put(randomChar, 1);
}
idx++;
}
// Test
System.out.println(Arrays.toString(randomResultArr));
for(Map.Entry<String, Integer> entry : randomResultMap.entrySet()) {
System.out.println(String.format("Key=%s, Count=%d", entry.getKey(), entry.getValue()));
}
}
}追问
谢谢
😁我还有很多java问题呢 你java好么 可以教我吗