1个回答
展开全部
输入御御内容没做去重,需要可以追问。
import java.util.Arrays;
public class Set {
//数组
private int[] list;
//数组使用长度
private int length;
//扩容方法
private void expand(){
int[] l = new int[2*list.length];
for(int i=0;i<list.length;i++){
l[i] = list[i];
}
list = l;
}
//并卜无参构造
Set(){
list = new int[20];
length = 0;
}
//有参构造
Set(int[] l){
set(l);
}
//设置数组方法
public void set(int[] l){
list = new int[20];
length = l.length;
if(length>list.length){
expand();
}
for(int i=0;i<length;i++){
list[i] = l[i];
}
}
//获取数组方法
public int[] get(){
int[] r = new int[length];
for(int i=0;i<length;i++){
r[i]=list[i];
}
return r;
}
//并方法
public Set merge(Set o){
int[] ol = o.get();
int[] r = new int[length+ol.length];
for(int i=0;i<length;i++){
r[i] = list[i];
}
int rlength = length;
for(int i=0;i<ol.length;i++){
boolean flag = true;
for(int j=0;j<length;j++){
if(ol[i]==list[j]){
flag = false;
break;
}
}
if(flag){
r[rlength++] = ol[i];
}
}
r = Arrays.copyOfRange(r, 0, rlength);
return new Set(r);
}
//交方法
public Set intersect(Set o){
int[] r = new int[length];
int rlength = 0;
int[] ol = o.get();
for(int i=0;i<length;i++){
for(int j=0;j<ol.length;j++){
if(list[i]==ol[j]){
r[rlength++] = list[i];
}
}
}
r = Arrays.copyOfRange(r, 0, rlength);
return new Set(r);
}
//输出结果,正常为toSring,如果要按题用soString,自己改
public String toString(){
return Arrays.toString(Arrays.copyOfRange(list, 0, length));
}
}
public class SetTest {
public static void main(String[] args) {
int[] a = new int[]{1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,21,23,25,27,29};
int[] b = new int[]{2,4,6,8,10,12,14,16,18,20,21,23,25,27,29,32,34,36,38,40};
Set s1 = new Set(a);
Set s2 = new Set(b);
System.out.println("集合一:"+s1.toString());
System.out.println("集合二:"+s2.toString());
System.out.println("集合并:"+s1.merge(s2).toString());
System.out.println("镇蔽岩集合交:"+s1.intersect(s2).toString());
}
}
import java.util.Arrays;
public class Set {
//数组
private int[] list;
//数组使用长度
private int length;
//扩容方法
private void expand(){
int[] l = new int[2*list.length];
for(int i=0;i<list.length;i++){
l[i] = list[i];
}
list = l;
}
//并卜无参构造
Set(){
list = new int[20];
length = 0;
}
//有参构造
Set(int[] l){
set(l);
}
//设置数组方法
public void set(int[] l){
list = new int[20];
length = l.length;
if(length>list.length){
expand();
}
for(int i=0;i<length;i++){
list[i] = l[i];
}
}
//获取数组方法
public int[] get(){
int[] r = new int[length];
for(int i=0;i<length;i++){
r[i]=list[i];
}
return r;
}
//并方法
public Set merge(Set o){
int[] ol = o.get();
int[] r = new int[length+ol.length];
for(int i=0;i<length;i++){
r[i] = list[i];
}
int rlength = length;
for(int i=0;i<ol.length;i++){
boolean flag = true;
for(int j=0;j<length;j++){
if(ol[i]==list[j]){
flag = false;
break;
}
}
if(flag){
r[rlength++] = ol[i];
}
}
r = Arrays.copyOfRange(r, 0, rlength);
return new Set(r);
}
//交方法
public Set intersect(Set o){
int[] r = new int[length];
int rlength = 0;
int[] ol = o.get();
for(int i=0;i<length;i++){
for(int j=0;j<ol.length;j++){
if(list[i]==ol[j]){
r[rlength++] = list[i];
}
}
}
r = Arrays.copyOfRange(r, 0, rlength);
return new Set(r);
}
//输出结果,正常为toSring,如果要按题用soString,自己改
public String toString(){
return Arrays.toString(Arrays.copyOfRange(list, 0, length));
}
}
public class SetTest {
public static void main(String[] args) {
int[] a = new int[]{1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,21,23,25,27,29};
int[] b = new int[]{2,4,6,8,10,12,14,16,18,20,21,23,25,27,29,32,34,36,38,40};
Set s1 = new Set(a);
Set s2 = new Set(b);
System.out.println("集合一:"+s1.toString());
System.out.println("集合二:"+s2.toString());
System.out.println("集合并:"+s1.merge(s2).toString());
System.out.println("镇蔽岩集合交:"+s1.intersect(s2).toString());
}
}
追问
我其实卡在数组那里,这种方法扩容,没看懂。什么意思。 list.length也不知道是多少?从后面推过来的吗?刚从上学期学了C语言,这学期刚学JAVA。。。
//扩容方法
private void expand(){
int[] l = new int[2*list.length];
for(int i=0;i<list.length;i++){
l[i] = list[i];
}
list = l;
}
追答
list.length是当前长度。调用此方法,说明长度不足,于是将长度翻倍。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询