0

I have an array with several elements String[] names = {"Jeremy", "Aude", "David"};

I would like to use the method .substring() to extract the index 1 and 2 namely the elements Aude and David then I want to concatenate the element Jeremy also.

Here is the result that I want to get : Aude, David, Jeremy

Do you have an idea to manipualte the elements ?

Here is my code

ArrayList<String> myList = new ArrayList<>();

        String[] names = {"Jeremy", "Aude", "David"};

        String x = "";

        for(int i = 0; i<names.length; i++){
            myList.add(names[i]);
            System.out.print(myList.get(i) + " ");
        }


        x = names.substring(1,2);
2
  • Seems like a job for Arrays.sort... Commented Dec 8, 2018 at 23:21
  • 1
    What does substring() have to do with this? The method is for extracting part of a string. What you want is simple array lookup by index, and has nothing whatsoever to do with substring. Commented Dec 8, 2018 at 23:29

4 Answers 4

1

You could do it pretty much as described, first create a new array of the same length as names (since you want to keep all of the names, just rotate them). Next, copy every name with an offset of one from names to that second array. Finally, copy the first name to the last element in the second array (and print it). Like,

String[] names = { "Jeremy", "Aude", "David" };
String[] names2 = new String[names.length];
System.arraycopy(names, 1, names2, 0, names.length - 1);
names2[names2.length - 1] = names[0];
System.out.println(Arrays.toString(names2));

Outputs (as requested)

[Aude, David, Jeremy]
Sign up to request clarification or add additional context in comments.

Comments

0

extract the index 1 and 2

names[1] and names[2]

element Jeremy also

names[0]

concatenate ... result that I want to get: Aude, David, Jeremy

String result = names[1] + ", " + names[2] + ", " + names[0];

would like to use the method .substring()

You can't, since that method is not for array manipulation.


Code

String[] names = {"Jeremy", "Aude", "David"};
String result = names[1] + ", " + names[2] + ", " + names[0];
System.out.println(result);

Output

Aude, David, Jeremy

Comments

0

I would not use .substring() to access the elements in an array. you can use their location in memory by getting them by their index.

ArrayList<String> myList = new ArrayList<>();

    String[] names = {"Jeremy", "Aude", "David"};

    String x = "";

    for(int i = 0; i<names.length; i++){
        myList.add(names[i]);
        System.out.print(myList.get(i) + " ");
    }


    x = names[1] + ", " names[2] + ", " names[0];

.substring() is used to get elements in an String such as

`String x = "jeremy;
 x = x.substring(0, 2);`

x will now equal to "jer"

I would also advise to use StringBuilder as it is mutable

Comments

0

The variable 'String [] names' is not an object of String but rather an object of the generic container class array, containing the object type String. This means that the method that you are trying to access is not usable due to being an array. You will have to make a new function that takes in the 2 parameters and returns a String.

public String subString(int index1,int index2)
{
     if(names.length < index1 || names.length < index2)
     {
          return names[index1] + ", " + names[index2];
     }
     return "Index's not in array range";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.