java中的字符串数组
我想在不使用arraylist的情况下解决这个问题。 我想将联系人添加到字符串数组中的特定索引中。然后以逗号分隔的字符串格式显示所有添加的联系人。 我的代码只给出最后添加的合同的结果: 联系人[第一位=鲍勃,最后一位=摩尔,电话号码=555-9756] 我的代码中的问题在哪里? 你知道怎么解决吗 该类由以下主要方法组成: 这是主要课程:java中的字符串数组,java,arrays,string,Java,Arrays,String,我想在不使用arraylist的情况下解决这个问题。 我想将联系人添加到字符串数组中的特定索引中。然后以逗号分隔的字符串格式显示所有添加的联系人。 我的代码只给出最后添加的合同的结果: 联系人[第一位=鲍勃,最后一位=摩尔,电话号码=555-9756] 我的代码中的问题在哪里? 你知道怎么解决吗 该类由以下主要方法组成: 这是主要课程: public class ExampleApp { public static void main(String[] args) {
public class ExampleApp {
public static void main(String[] args) {
PhoneBook pb = new PhoneBook("Personal book");
System.out.println( pb.getName() );
pb.add("Alice", "Green", "555-1234");
pb.add("Mary", "Smith", "555-6784");
pb.add("Bob", "Moore", "555-9756");
System.out.println( pb.toString() );// here i want to display all the contracts seperated by commas
System.out.println( pb.first() );// first contract
System.out.println( pb.get(2) );// second contract
String toBeFound = new String("Moore");
System.out.println( pb.find(toBeFound) );// display the found contract
}
}
这是电话簿类:
public class PhoneBook {
public static final int MAX = 10;
public String name;
String[] contracts = new String[MAX]; // i created an array of strings
Contact c;
/**
* Create a new phonebook with given name
*/
public PhoneBook(String name) {
this.name = name;
}
/**
* Return the phonebook name
*/
public String getName() {
return name;
}
/**
* Insert a new contact at the end
*/
public void add(String first, String last, String number){
c=new Contact(first,last,number);
for(int i=0;i<MAX;i++){ // i added for each array index the contracts strings
contracts[i]= c.toString();
}
}
/**
* Return the first contact
*/
public String first() {
return get(1);
}
/**
* Return the i-th contact (supposing that first
* index is 1)
*/
public String get(int i) {
String s =contracts[i].toString();
return s;
}
/**
* Return a string containing the list of textual
* representation of all contacts, separated by ", ".
* List starts with "("and ends with ")"
*/
public String toString() {
String s= " ";
for(int i=1;i<MAX;i++){ // here i tried to display the string looping the array
s=contracts[i].toString();
}
return s;
}
/**
* Return the textual representation of first
* contact containing "needle"
*/
public String find(String needle) {
//TODO: to be implemented
return null;
}
}
您应该跟踪添加到阵列中的最后一个联系人:
private int lastIndex = 0; // index of first available index of the array
...
/**
* Insert a new contact at the end
*/
public void add(String first, String last, String number){
c=new Contact(first,last,number);
if (lastIndex < MAX)
contracts[lastIndex++]= c.toString();
}
调用toString for contracts[i]是多余的,因为它已经是一个字符串了。也许您想在数组中存储联系人实例而不是字符串?这会更有意义
添加一个额外变量static int Last=0;跟踪已添加的联系人数量,并将添加功能更新为
public void add(String first, String last, String number){
if(Last>=MAX){
System.out.println("Error in adding\n");
}
else{
c=new Contact(first,last,number);
contacts[Last] = c.toString();
Last++;
}
}
将Tostring for loop更改为last。因此,您将只打印添加的联系人。在您的情况下,如果添加的联系人数量较少,则打印到最大值是错误的
必须将i=0,1,2,…,MAX-1传递给geti函数。数组是基于零的索引数组。
您可以使用ArrayList。这会对你有很大帮助,你应该把你的veriables保密
private String first;
private String last;
private String number;
private ArrayList<String> list = new ArrayList<String>();
for(String pointer : list){
}
您总是删除最后一个toString方法。你的Add方法是错误的。您总是打开0并在数组中再次写入。不确定我是否正确理解了您的所有代码,但我认为您正在覆盖电话簿中的所有其他联系人:
public void add(String first, String last, String number){
c=new Contact(first,last,number); //(1)
for(int i=0;i<MAX;i++){ //(2)
contracts[i]= c.toString();
}
}
在位置1处,将创建一个新的接触对象并将其指定给c。在下一步2中,循环遍历数组,并将c中包含的信息分配给添加到contracts数组中所有已有条目的最新联系人
与您的问题无关,我建议您将固定大小的数组替换为Contact类型的ArrayList。向该列表添加条目、迭代、排序等都非常简单 因此,您遇到的问题是,无论您尝试获取的是哪一个联系人,您总是检索最后一个联系人。这是因为当你添加一个新的联系人时,你实际上是在替换所有的联系人,使他们都一样。您正在从电话簿中获得正确的联系人,但它们都具有相同的值 要修复它,请执行以下操作:
public class PhoneBook
{
int contactsAdded = 0; // Add an integer to store how many contacts you have added
Contact[] contacts = new Contact[MAX]; //Change this to Contact array, not string
//Contact c; //You can remove this line
//Rest of your code
}
public void add(String first, String last, String number)
{
//Only add if the number of contacts is less than the max
if (contactsAdded < MAX)
{
//Construct the new contact when you use it.
contacts[contactsAdded] = new Contact(first, last, number);
contactsAdded++;
}
}
参数类型字符串的运算符<未定义,int@Niranjan抱歉,我没有注意到该方法中已经有最后一个参数。将它重命名为lastIndex。你能给我解释更多关于toString函数的内容吗;公共字符串toString{String s=*;forint i=0;如果我使用此方法,则结果中会出现nullpointerexception!例如,您只向电话簿添加了5个联系人。您正在尝试访问所有MAX contacts。这不是必需的,您可以使用上一个索引来停止它。我希望显示所有添加的合同:但这些方法仅返回1个合同:在pb.toString方法调用中。为什么?谢谢你的回答,但在这里,我尝试在不使用arraylistforint i=0的情况下求解;i@Niranjan你的add方法和toString方法错误我更改了你的代码,你的方法是删除旧的保存。你总是返回0并再次写入你添加nira,然后添加nira2,但第二次删除nira并编写nira2 nira2。我更改了您的代码并再次发送。请在add方法中查找您的for循环,它总是转到索引0并在其上写入感谢您的帮助!我解决了问题:您的代码工作正常!问题在于toString方法中的循环!
private ArrayList<Contact> list = new ArrayList<Contact>();
public Contact(String first, String last, String number) {
this.first=first;
this.last = last;
this.number=number;
}
public void add(String first, String last, String number){
c=new Contact(first,last,number);
list.add(c);
}
public class PhoneBook {
public static final int MAX = 10;
public String name;
String[] contracts = new String[MAX]; // i created an array of strings
Contact c;
private int count = 0;// saved last index of array
public PhoneBook(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void add(String first, String last, String number) {
c = new Contact(first, last, number);
contracts[count] = c.toString(); // save your String inside of last index++
count++;
}
public String first() {
return get(1);
}
public String get(int i) {
String s = contracts[i].toString();
return s;
}
public String toString() {
for (int i = 0; i < MAX; i++) {
if (contracts[i] != null)
System.out.println(contracts[i].toString());
}
return "";
}
public String find(String needle) {
return null;
}
}
public class Contact {
public String first;
public String last;
public String number;
public Contact(String first, String last, String number) {
this.first = first;
this.last = last;
this.number = number;
}
@Override
public String toString() {
return "Contact [first=" + first + ", last=" + last + ", number="
+ number + "]";
}
}
//Personal book
//Contact [first=Alice, last=Green, number=555-1234]
//Contact[first=Mary, last=Smith, number=555-6784]
//Contact [first=Bob, last=Moore, number=555-9756]
public void add(String first, String last, String number){
c=new Contact(first,last,number); //(1)
for(int i=0;i<MAX;i++){ //(2)
contracts[i]= c.toString();
}
public class PhoneBook
{
int contactsAdded = 0; // Add an integer to store how many contacts you have added
Contact[] contacts = new Contact[MAX]; //Change this to Contact array, not string
//Contact c; //You can remove this line
//Rest of your code
}
public void add(String first, String last, String number)
{
//Only add if the number of contacts is less than the max
if (contactsAdded < MAX)
{
//Construct the new contact when you use it.
contacts[contactsAdded] = new Contact(first, last, number);
contactsAdded++;
}
}