前两个我帮你做出来了 最后一个不知道你要的结果是怎么样所以就不帮你做了, 如果不知道,写清楚需要什么我再帮你写 public class NameSystem1 { public List BMoreToA(String[] a, String[] b) { List temp = new ArrayList(); for (int i = 0; i < b.length; i++) { boolean flag = false; for (int j = 0; j < a.length; j++) { if (b[i].equals(a[j])) { flag = true; break; } else { flag = false; } } if (!flag) { temp.add(b[i]); } } return temp; } public static void main(String[] args) { NameSystem1 test = new NameSystem1(); String[] a = { "x", "y", "xy", "yx" }; String[] b = { "xy", "y", "yx", "a", "b" }; List temp = null; temp = test.BMoreToA(a, b); System.out.println("B比A多的字符串"); for (int i = 0; i < temp.size(); i++) { System.out.print(temp.get(i) + " "); } System.out.println(""); System.out.println("B比A少的字符串"); temp = test.BMoreToA(b, a); for (int i = 0; i < temp.size(); i++) { System.out.print(temp.get(i) + " "); } } }
public List BDifferAOrder(String[] a, String[] b) { List temp = new ArrayList(); for (int i = 0; i < b.length; i++) { for (int j = 0; j < a.length; j++) { if(b[i].equals(a[j])){ if(i!=j){ temp.add(b[i]); } break; } } } return temp; }给你吧只是有个问题的是,你提供的字符串数组中最好不要有相同的字符串,我没有去判断这种情况 你的思路清晰了就自己写代码吧,如果觉得我写的有错指出来我再看看
String[] a = { "x", "y", "xy", "yx" }; String[] b = { "xy", "y", "yx", "a", "b" }; for (int i = 0; i < b.length; i++) { for (int j = 0; j < a.length; j++) { if (b[i].equals(a[j])) { b[i] = ","; a[j] = ","; } } } System.out.println("b比a少哪些字符串"); for (String var : a) { if (!",".equals(var)) System.out.println(var); } System.out.println("b比a多哪些字符串"); for (String var : b) { if (!",".equals(var)) System.out.println(var); } 第三问题目不明确
1、b比a多哪些字符串?
2、b比a少哪些字符串?
3、b与a中顺序不同的字符串有哪些?
2、b比a少哪些字符串? 指的是:{"x"}
3、b与a中顺序不同的字符串有哪些?指的是:{"xy","yx"}当然,数组a,b只是个小例子而已,请大家不要误会。
如果有人知道解决问题的思路,可以说说,其他的话就别讲了,免得浪费大家时间。
第二,1,2个问题不就是集合的B-A,A-B的问题。
对于第三个问题,我问个最简单的
a={ab,cd,ef}
b={ef,cd,ab}
这时候,你想要什么输出。
你想过你这个问题本身存在的逻辑漏洞么?该问题存在唯一确定答案么?如果没有,请想清楚以后再说话。
记得api里有提供计算2集合之间各种关系的方法
b={"xy","y","yx"}????
String[] b={"xy","y","yx","a","b"}
{"xy","yx"}lz意思应该是a和b中,字符种类个数一样但顺序不一样的字符串
最后一个不知道你要的结果是怎么样所以就不帮你做了,
如果不知道,写清楚需要什么我再帮你写
public class NameSystem1 { public List BMoreToA(String[] a, String[] b) {
List temp = new ArrayList();
for (int i = 0; i < b.length; i++) {
boolean flag = false;
for (int j = 0; j < a.length; j++) {
if (b[i].equals(a[j])) {
flag = true;
break;
} else {
flag = false;
}
}
if (!flag) {
temp.add(b[i]);
}
}
return temp;
} public static void main(String[] args) {
NameSystem1 test = new NameSystem1(); String[] a = { "x", "y", "xy", "yx" }; String[] b = { "xy", "y", "yx", "a", "b" };
List temp = null;
temp = test.BMoreToA(a, b);
System.out.println("B比A多的字符串");
for (int i = 0; i < temp.size(); i++) {
System.out.print(temp.get(i) + " ");
}
System.out.println("");
System.out.println("B比A少的字符串");
temp = test.BMoreToA(b, a);
for (int i = 0; i < temp.size(); i++) {
System.out.print(temp.get(i) + " ");
}
}
}
第三个问题:以a作为基准,b和a进行比较,
(在数组a,数组b中所有相同元素进行比较顺序)
取得b中数组元素与a中数组元素顺序不同的元素。比如:a={"x","y","xy","yx"},b={"xy","y","yx","a","b"}
数组a,b相同的元素是:{"y","xy","yx"}
那么比较它们在数组a,b中的顺序,以数组a作为基准,数组b和a进行比较
最后得出顺序不同的数组元素:{"xy","yx"}
16楼的对第三个问题的回答是正确的。
public List BDifferAOrder(String[] a, String[] b) {
List temp = new ArrayList();
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < a.length; j++) {
if(b[i].equals(a[j])){
if(i!=j){
temp.add(b[i]);
}
break;
}
}
}
return temp;
}给你吧只是有个问题的是,你提供的字符串数组中最好不要有相同的字符串,我没有去判断这种情况
你的思路清晰了就自己写代码吧,如果觉得我写的有错指出来我再看看
最多就是效率的取舍上有些区别第3个问题么实在是描述的有点简单,改成:a,b相同的元素中,b与a比较,顺序不同的是哪些?这样比较好吧
但是也不难啊,反正数组有下标,比较下就可以了,随便放在什么容器下
你来100万个数据也可以。速度非常快。
方法多了去了。你的态度就不想给你回答
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;/**
* Author: db2admin
* Date : 2010-2-4
* Time : 15:41:37
* Comment:
*/public class test { public static List toArrayList(String[] temp) {
List templist=new ArrayList();
for (int i = 0; i < temp.length; i++) {
templist.add(temp[i]);
}
return templist;
} public static List romove(List lista,List listb){
lista.removeAll(listb);
return lista;
//System.out.println(lista);
} public static List compareArr(String[] a,String[] b){
List commonlist=new ArrayList();
if(a.length<b.length){
for(int i=0;i<a.length;i++){
if(a[i].equals(b[i]))
commonlist.add(a[i]);
}
}
return commonlist;
}
public static void main(String[] args){
String[] a = {"x", "y", "xy", "yx"};
String[] b = {"xy", "y", "yx", "a", "b"};
// A-B
System.out.println(test.romove(test.toArrayList(a),test.toArrayList(b)));
// B-A
System.out.println(test.romove(test.toArrayList(b),test.toArrayList(a)));
// A-(A-B) 公共集
System.out.println(
test.romove(test.toArrayList(a),test.romove(test.toArrayList(a),test.toArrayList(b)))
);
// 公共集-顺序值相同集
System.out.println(
test.romove(
test.romove(test.toArrayList(a),test.romove(test.toArrayList(a),test.toArrayList(b))),
test.compareArr(a,b))
);
}
}
简单实现
所以,这里小小的提点你一下吧
其实,不管数组多长,3次遍历就可以搞定,时间复杂度是两者长度的和
至于怎么做,你自己去想吧。至于第三个问题,确实有很多种理解
按照最复杂的理解,就是说两个字符串,只有当它们每种出现的每个字母总共出现的次数完全一致,而且整串不同时,才是这个顺序不同;
这个我认为,设计个数据结构,小小的改变一下最终的比较方式
这个肯定要遍历到每个字母,这是第一次遍历,遍历完之后就会回到跟前面两个相似的问题所以,总的来说,也不会很难至于具体怎么做,自己想吧,既然你敢那么问,想来应该是有好办法了,我就不献丑了。
用SQL来实现就很简单了
顺序表 + for循环
比如第一问:先用数组a中一个跟后面比较,有的话用flag标记,遍历完后,把没有的输出,
其他两问一样~~
String[] b = { "xy", "y", "yx", "a", "b" }; for (int i = 0; i < b.length; i++) {
for (int j = 0; j < a.length; j++) {
if (b[i].equals(a[j])) {
b[i] = ",";
a[j] = ",";
}
}
}
System.out.println("b比a少哪些字符串");
for (String var : a) {
if (!",".equals(var))
System.out.println(var);
}
System.out.println("b比a多哪些字符串");
for (String var : b) {
if (!",".equals(var))
System.out.println(var);
}
第三问题目不明确
大数据也暴力
对大数据有更好的方法嘛?PS:LZ的态度确实不好~~~
for(int i=0;i<a.length;i++){
for(int j=0;j<b.length;j++)
{
a[i]==b[j];
return a[i];
}
}
再说说顺序不同:将a中字符串与b中比较,相等但是位置不相同记录下来,这就是位置不同;伪代码如下:
a[i]==b[j] && i==j;
{
string arr1 = "";
for (int i = 0; i < a.Length; i++)
{
bool flag = false;
for (int j = 0; j < b.Length; j++)
{
if (a[i].Equals(b[j]))
{
flag = true;
} }
if (!flag)
{
arr1 += a[i] + " , ";
}
}
return arr1;
}
}在要看效果的地方调用方法
String[] a ={ "x", "y", "xy", "yx" };
String[] b ={ "xy", "y", "yx", "a", "b" }; Response.Write("B比A多:" + CompString(b,a)+"<br>");
Response.Write("B比A少:" + CompString(a,b)+ "<br>");